Home > OS >  The argument type 'Object?' can't be assigned to the parameter type 'List'
The argument type 'Object?' can't be assigned to the parameter type 'List'

Time:10-22

On older flutter sdk version below example is working, but now I have error The argument type 'Object?' can't be assigned to the parameter type 'List'

FutureBuilder(
        future: fetchEmp(),
        builder: (context, snapshot) {
          return snapshot.hasData
              ? RecommandEmp(emps: snapshot.data)  ---Here is problem!!!
              : Center(child: Image.asset('assets/images/no.png'));
        },
    ),



    class RecommandEmp extends StatefulWidget {
    
      const RecommandEmp({Key? key, required this.emps}) : super(key: key);
    
      final List<Emp> emps;
      @override
      _RecommandEmpState createState() => _RecommandEmpState();
    }

fetchEmp

Future<List<Emp>> fetchEmp() async {

  final response = await http.get(Uri.https(myUrl, 'select/emp'));

  if (response.statusCode == 200) {

    List<Emp> emp = (json.decode(utf8.decode(response.bodyBytes))["items"]  as List)
        .map((data) => Emp.fromJson(data))
        .toList();
    return emp;
  } else {

    throw Exception('Error!');
  }
}

model --> https://ibb.co/zr7H8v2

CodePudding user response:

snapshot.data will always be by default of type Object unless you specify the type, which can be done in two different ways

First as below

FutureBuilder<List<Emp>>(
      future: fetchEmp(),
      builder: (context, snapshot) {
        return snapshot.hasData
            ? RecommandEmp(emps: snapshot!.data) 
            : Center(child: Image.asset('assets/images/no.png'));
      },
    ),

or the second (for the unlikely case that you're not sure of your type i.e. returns dynamic) :

FutureBuilder(
      future: fetchEmp(),
      builder: (context, snapshot) {
        return snapshot.hasData && snapshot is List<Emp>
            ? RecommandEmp(emps: snapshot.data as List<Emp>) 
            : Center(child: Image.asset('assets/images/no.png'));
      },
    ),

CodePudding user response:

The method groupedTransactionValues is returning a map of Object. Rather, return a map of dynamic which is a runtime determined type. Change the line:

List<Map<String, Object>> get groupedTransactionValues {

to:

List<Map<String, dynamic>> get groupedTransactionValues {
  • Related