The argument type 'Future<dynamic>' can't be assigned to the parameter type 'Future<Object>?'

Viewed 2977

The Error:

lib/pages/view_page.dart:90:21: Error: The argument type 'Future' can't be assigned to the parameter type 'Future?'
because 'dynamic' is nullable and 'Object' isn't.

  • 'Future' is from 'dart:async'.
  • 'Object' is from 'dart:core'. future: generateEmployeeList(),

The codes which have a specific error

@override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('Syncfusion flutter datagrid'),
            ),
            body: FutureBuilder<Object> (
                future: generateEmployeeList(),
                builder: (context, data) {
                  return data.hasData
                      ? SfDataGrid(
                          source: employeeDataSource,
                          columns: _columns,
                          columnWidthMode: ColumnWidthMode.fill)
                      : Center(
                          child: CircularProgressIndicator(
                          strokeWidth: 2,
                          value: 0.8,
                        ));
                }));
      }

Future codes:

        Future<dynamic> generateEmployeeList() async {
            // Give your PHP URL. It may be online URL o local host URL.
            // Follow the steps provided in the below KB to configure the mysql using
            // XAMPP and get the local host php link,
            ///
            var url = "my_url";
            final response = await http.get(Uri.parse(url));
            var list = json.decode(response.body);
        
            // Convert the JSON to List collection.
            List<Employee> _employees =
                await list.map<Employee>((json) => Employee.fromJson(json)).toList();
            employeeDataSource = EmployeeDataSource(_employees);
            return _employees;
          }

Question: How can I assign The argument type 'Future' to the parameter type 'Future?'

1 Answers

The error is because you have FutureBuilder<Object>, but generateEmployeeList() has a return type of Future<dynamic>. Type dynamic can be null, but Object can't be null.

Since you are returning a List<Employee> value from the future, solve the problem like this:

body: FutureBuilder<List<Employee>> 

And then:

Future<List<Employee>> generateEmployeeList() async {
  ...
  return Future.value(_employees);
}
Related