Home > database >  Too many positional arguments: 0 expected, but 6 found flutter
Too many positional arguments: 0 expected, but 6 found flutter

Time:01-17

I am having a user model class that looks like this below

class User {
  String? email;
  String? about;
  String? name;
  String? picture;
  int? index;
  String? imageFetchType;

  User({this.email, this.about, this.name, this.picture, this.index, this.imageFetchType});

  factory User.fromJson(Map<String, dynamic> json) => User(
    email: json["email"],
    about: json["about"],
    name: json["name"],
    picture: json["picture"],
    index: json["index"],
    imageFetchType: json["imageFetchType"]
  );

  Map<String, dynamic> toJson() => {
    "email": email,
    "about": about,
    "name": name,
    "picture": picture,
    "index": index,
    "imageFetchType": imageFetchType
  };
} 

And below is a Future function that i am using to fetch users list and looping through the list to populate users

 Future<List<User>> _fetchUsersListUsingLoop() async {
    try {
     
      var response = await http.get(
          Uri.parse(
              "https://api.json-generator.com/templates/Eh5AlPjYVv6C/data"),
          headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer tltsp6dmnbif01jy9xfo9ssn4620u89xhuwcm5t3",
          });


      List<User> usersList = [];

      for (var u in json.decode(response.body)) {
        User user = User(u["email"], u["about"], u["name"], u["picture"],
            u["index"], u["imageFetchType"]);

        usersList.add(user);
      }

      return usersList;
    } catch (e) {
      log("FetchUsersListUsingLoopException $e");

      rethrow;
    }
  }

I am getting Too many positional arguments: 0 expected, but 6 found. (Documentation) Try removing the extra positional arguments, or specifying the name for named arguments. in below line

User user = User(u["email"], u["about"], u["name"], u["picture"],
            u["index"], u["imageFetchType"]);

When i try to change my constructor in user class to below its working


User(this.email, this.about, this.name, this.picture, this.index, this.imageFetchType);

But i get another error in fromJson that says 6 positional argument(s) expected, but 0 found. (Documentation) Try adding the missing arguments. in the below lines of code

factory User.fromJson(Map<String, dynamic> json) => User(
    email: json["email"],
    about: json["about"],
    name: json["name"],
    picture: json["picture"],
    index: json["index"],
    imageFetchType: json["imageFetchType"]
  );

How can i be able to use the constructor User({this.email, this.about, this.name, this.picture, this.index, this.imageFetchType}); in the user class and loop without getting Too many positional arguments: 0 expected, but 6 found error

CodePudding user response:

if server response is jsonArray, after jsonDecoding jsonArrary. you have to convert your model(List of model) by using map..

 Future<List<User>> _fetchUsersListUsingLoop() async {
try {
  var response = await http.get(
      Uri.parse(
          "https://api.json-generator.com/templates/Eh5AlPjYVv6C/data"),
      headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer tltsp6dmnbif01jy9xfo9ssn4620u89xhuwcm5t3",
      });


  List jsonResponse = json.decode(response.body);
 List<User> usersList =  jsonResponse.map((data) =>                                            User.fromJson(data)).toList();
         print(usersList );
  return usersList;
} catch (e) {
  log("FetchUsersListUsingLoopException $e");

  rethrow;
}

}

CodePudding user response:

You can create two extra method toJsonUsers and fromJsonUsers to extract the exact objects and json. See below.

class User {
  String? email;
  String? about;
  String? name;
  String? picture;
  int? index;
  String? imageFetchType;

  User({this.email, this.about, this.name, this.picture, this.index, this.imageFetchType});

  factory User.fromJson(Map<String, dynamic> json) => User(
    email: json["email"],
    about: json["about"],
    name: json["name"],
    picture: json["picture"],
    index: json["index"],
    imageFetchType: json["imageFetchType"]
  );

  Map<String, dynamic> toJson() => {
    "email": email,
    "about": about,
    "name": name,
    "picture": picture,
    "index": index,
    "imageFetchType": imageFetchType
  };
} 

String toJsonUsers(List<User> users) {
  List<Map<String, dynamic>> jsonData = users.map((user) => user.toMap()).toList();
  return jsonEncode(jsonData);
}

List<User> fromJsonUsers(String json) {
  List<Map<String, dynamic>> jsonData = jsonDecode(json);
  return jsonData.map((map) => User.fromMap(map)).toList();
}
  •  Tags:  
  • Related