颤动中的脏状态错误

时间:2018-08-23 04:00:15

标签: dart flutter

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,
    );
  }
}

在我的Flutter项目中,我刚刚遇到以下错误。 I / flutter(12737):W小工具库引起的异常CA ═════════════════════════ I / flutter(12737):在构建FutureBuilder>( dirty,state: 我/颤振(12737):_FutureBuilderState>#b09b6): I / flutter(12737):类型'NoSuchMethodError'不是类型'String'的子类型 I / flutter(12737):要么断言表明框架本身存在错误,要么我们应该提供 I / flutter(12737):此错误消息中的更多信息可帮助您确定和解决根本原因。 I / flutter(12737):无论哪种情况,请通过在GitHub上提交错误来报告此断言:

什么是脏状态?

1 个答案:

答案 0 :(得分:0)

正如错误提示NoSuchMethodError所示,您已经将Future的结果分配给了一个变量,但是FutureBuilder期望此构建器当前连接到的将来的异步计算,而depState不是一种方法。您可以在future: fetchUserInfo

中直接设置FutureBuilder

尝试一下,

@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,
    );
  }