Flutter中的脏状态错误

vawmfj5a  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(133)

第五个.省道

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):由小工具库捕获的异常情况I/flutter(12737):构建FutureBuilder〉(**脏,状态:**I/flutter(12737))时抛出以下Assert:_未来生成器状态〉#b09b6):I/扑动(12737):类型“NoSuchMethodError”不是类型“String”的子类型I/flutter(12737):Assert要么表明框架本身存在错误,要么我们应该提供大量的I/flutter(12737):此错误消息中的详细信息,以帮助您确定并修复根本原因。I/flutter(12737):无论哪种情况,请在GitHub上提交bug来报告这个Assert:
什么是脏状态?

mqkwyuun

mqkwyuun1#

如错误提示NoSuchMethodError所示,您已将Future的结果赋给变量,但FutureBuilder期望此生成器当前连接到的未来异步计算,并且depState不是方法。您可以直接在FutureBuilder中设置future: fetchUserInfo
试试这个

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

相关问题