1

I watched this tutorial but im stuck in return of future list https://www.youtube.com/watch?v=lTKFYGHx16M trying to connect mysql php api and flutter code

import '../model/usersdata.dart';
import 'package:http/http.dart' as http;

Future<List<Userprofile>> fetchUsers() async {
  var url = Uri.parse("http://192.168.100.68/mlm_sys/api_request/get_data.php");
  final response = await http.get(url);
  print(response.body);
  return userprofileFromJson(response.body);<----ERROR LINE

ERROR: //A value of type 'Userprofile' can't be returned from the function 'fetchUsers' because it has a return type of 'Future<List>'. }

///=====MODEL

import 'dart:convert';

Userprofile userprofileFromJson(String str) =>
    Userprofile.fromJson(json.decode(str));

String userprofileToJson(Userprofile data) => json.encode(data.toJson());

class Userprofile {
  Userprofile({
    required this.id,
    required this.sponsor,
    required this.fname,
    required this.lname,
    required this.mname,
    required this.pnum,
    required this.address,
    required this.email,
    required this.username,
    required this.password,
    required this.dateRegistration,
  });

........AND SO ON
4
  • 1
    add how you use fetchUsers() function Commented Aug 24, 2021 at 20:03
  • Userprofile is not a List of Userprofile... you need to map your response and turn into a list Commented Aug 24, 2021 at 20:08
  • @NirmalCode il use the future builder later in my widget Commented Aug 24, 2021 at 20:17
  • @MarianoZorrilla i have map in my model is this what you means? Map<String, dynamic> toJson() => { "id": id, "sponsor": sponsor, "fname": fname, "lname": lname, "mname": mname, "pnum": pnum, "address": address, "email": email, "username": username, "password": password, "date_registration": "${dateRegistration.year.toString().padLeft(4, '0')}-${dateRegistration.month.toString().padLeft(2, '0')}-${dateRegistration.day.toString().padLeft(2, '0')}", }; Commented Aug 24, 2021 at 20:18

2 Answers 2

2

The fetchUsers() is expected to return a list of user profiles, but the function userprofileFromJson() returns only a user profile object. You may update the fetch users function to be like this (considering the response.body is a list of user profiles).

Future<List<Userprofile>> fetchUsers() async {
    var url = Uri.parse("http://192.168.100.68/mlm_sys/api_request/get_data.php");
    final http.Response response = await http.get(url);
    print(response.body);
    /********* the return statement needs to be like this ******/
    return List<Userprofile>.from(
        (json.decode(response.body) as List).map((jsonString) => userprofileFromJson(jsonString)));
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Okay iget it now. Thank you so much
0

Your method called userprofileFromJson returns a map, not a list. You should use this converter to create this UserProfile class agait. You can copy a sample json file that your server sends and paste it. After that, you can copy the finished dart code and paste it instead of your code. Finally, type await between return and the returned value like this:

Future<List<Userprofile>> fetchUsers() async {
  var url = Uri.parse("http://192.168.100.68/mlm_sys/api_request/get_data.php");
  final response = await http.get(url);
  print(response.body);
  return await userprofileFromJson(response.body);

The difference is that you don't return a future with await. You return the real value after it arrives.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.