6 positional argument(s) expected, but 1 found. Try Adding the Missing FLUTTER

Asked

Viewed 81 times

-2

I’m developing a screen that sends information to a Rest C api#

However from the following error

6 positional argument(s) expected, but 1 found. Try Adding the Missing

Follows code:


import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<Avaliacao> createAvaliacao(String nome, String cpf, String celular,
    String nota_Estab, String voltaria, String opniao) async {
  final http.Response response = await http.post(
    'https://10.0.2.2:44373/api/avaliacao',
    headers: <String, String>
    
    {
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      'nome': nome,
      'cpf': cpf,
      'celular': celular,
      'nota_Estab': nota_Estab,
      'voltaria': voltaria,
      'opniao': opniao
    }),
  );

  if (response.statusCode == 201) {
    return Avaliacao.fromJson(jsonDecode(response.body));
  } else {
    throw Exception('Falha ao enviar , tente novamente ');
  }
}

class Avaliacao {
  final int id_avaliacao;
  final String nome;
  final String cpf;
  final String celular;
  final String nota_Estab;
  final String voltaria;
  final String opniao;

  Avaliacao(
      {this.id_avaliacao,
      this.nome,
      this.cpf,
      this.celular,
      this.nota_Estab,
      this.voltaria,
      this.opniao});

  factory Avaliacao.fromJson(Map<String, dynamic> json) {
    return Avaliacao(
      id_avaliacao: json['ID_avaliacao'],
      nome: json['Nome'],
      cpf: json['CPF'],
      celular: json['Celular'],
      nota_Estab: json['Nota_Estab'],
      voltaria: json['Voltaria'],
      opniao: json['Opniao'],
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  final TextEditingController _controller = TextEditingController();
  Future<Avaliacao> _futureAvaliacao;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Avaliação',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Enviar Avaliação '),
        ),
        body: Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.all(8.0),
          child: (_futureAvaliacao == null)
              ? Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    TextField(
                      controller: _controller,
                      decoration: InputDecoration(hintText: 'Nome'),
                    ),
                    TextField(
                      controller: _controller,
                      decoration: InputDecoration(hintText: 'CPF'),
                    ),
                    TextField(
                      controller: _controller,
                      decoration: InputDecoration(hintText: 'Celular'),
                    ),
                    TextField(
                      controller: _controller,
                      decoration: InputDecoration(
                          hintText: 'Nota para o estabelecimento'),
                    ),
                    TextField(
                      controller: _controller,
                      decoration: InputDecoration(
                          hintText: 'Voltaria ao estabelecimento?'),
                    ),
                    TextField(
                      controller: _controller,
                      decoration: InputDecoration(
                          hintText: 'Opnião do estabelecimento'),
                    ),
                    ElevatedButton(
                      child: Text('Enviar'),
                      onPressed: () {
                        setState(() {
                          _futureAvaliacao = createAvaliacao(_controller.text);
                        });
                      },
                    ),
                  ],
                )
              : FutureBuilder<Avaliacao>(
                  future: _futureAvaliacao,
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return Text(snapshot.data.nome);
                    } else if (snapshot.hasError) {
                      return Text("${snapshot.error}");
                    }

                    return CircularProgressIndicator();
                  },
                ),
        ),
      ),
    );
  }
}

1 answer

1

As the error itself says "You are trying to call a method that has 6 mandatory parameters, but you are only reporting one of them."

It’s probably this method here:

Future<Avaliacao> createAvaliacao(String nome, String cpf, String celular,
    String nota_Estab, String voltaria, String opniao) async {
  final http.Response response = await http.post(
    'https://10.0.2.2:44373/api/avaliacao',
    headers: <String, String>
    
    {
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      'nome': nome,
      'cpf': cpf,
      'celular': celular,
      'nota_Estab': nota_Estab,
      'voltaria': voltaria,
      'opniao': opniao
    }),
  );

  if (response.statusCode == 201) {
    return Avaliacao.fromJson(jsonDecode(response.body));
  } else {
    throw Exception('Falha ao enviar , tente novamente ');
  }
}

You make his call the following way createAvaliacao(_controller.text), you need to enter the other parameters, or leave them as not mandatory.

To leave them optional just between keys {}:

Future<Avaliacao> createAvaliacao({String nome, String cpf, String celular,
        String nota_Estab, String voltaria, String opniao}) async {
}

And to make the call:

createAvaliacao(nome: "meu nome", cpf: "meu cpf");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.