fifth.dart

import 'package:flutter/material.dart';
import 'package:emas_app/Dependant.dart' as Dep;
import 'package:emas_app/crm_dependent_list_model.dart';
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:emas_app/crm_dep_entitlement_model.dart';

final String url = "http://crm.emastpa.com.my/MemberInfo.json";

//Future to get list of dependent names
Future<List<DependantModel>> fetchUserInfo() async{

  http.Response response = await http.get(url);
  var responsejson = json.decode(response.body);

  return(responsejson[0]['Dependents'] as List)
      .map((user) => DependantModel.fromJson(user))
      .toList();
}

class Fifth extends StatefulWidget {
  @override
  _FifthState createState() => _FifthState();
}

class _FifthState extends State<Fifth> {

  static Future<List<DependantModel>> depState;

  @override
  void initState() {
    depState = fetchUserInfo();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    //ListView.builder inside FutureBuilder
    var futureBuilder = new FutureBuilder<List<DependantModel>>(
        future: depState,
        builder: (context, snapshot){
          switch(snapshot.connectionState){
            case ConnectionState.none:
            case ConnectionState.waiting:
              return new Center(
                child: new CircularProgressIndicator(),
              );
            default:
              if(snapshot.hasError){
                return new Text(snapshot.error);
              }else{

                List<DependantModel> user = snapshot.data;

                return new ListView.builder(
                    itemCount: user.length,
                    itemBuilder: (context, index){
                      return new Column(
                        children: <Widget>[
                          new ListTile(
                            title: new Text(user[index].name,
                                style: TextStyle(fontSize: 20.0)),
                            subtitle: new Text(user[index].relationship,
                                style: TextStyle(fontSize: 15.0)),
                            trailing: new MaterialButton(color: Colors.greenAccent,
                                textColor: Colors.white,
                                child: new Text("More"),
                                onPressed: (){
                                  Navigator.push(context,
                                      new MaterialPageRoute(builder: (context) => Dep.Dependents(name: user[index].name)));
                                }
                            ),
                          )
                        ],
                      );
                    });
              }
          }
        });

    return new Scaffold(
      body: futureBuilder,
    );
  }
}

I've just had the following error occurring in my flutter project. I/flutter (12737): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (12737): The following assertion was thrown building FutureBuilder>(dirty, state: I/flutter (12737): _FutureBuilderState>#b09b6): I/flutter (12737): type 'NoSuchMethodError' is not a subtype of type 'String' I/flutter (12737): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter (12737): more information in this error message to help you determine and fix the underlying cause. I/flutter (12737): In either case, please report this assertion by filing a bug on GitHub:

what is dirty state?


Solution 1: Raj Yadav

As the error suggests, NoSuchMethodError, you have assigned the results of Future to a variable but the FutureBuilder expects future asynchronous computation to which this builder is currently connected and depState is not a method. You can directly set future: fetchUserInfo in FutureBuilder

Try this,

@override
  Widget build(BuildContext context) {    
   var futureBuilder = new FutureBuilder<List<DependantModel>>(
        future: fetchUserInfo, //your Future method
        builder: (context, snapshot){
          //Your code goes here
          }
        });

    return new Scaffold(
      body: futureBuilder,
    );
  }