【问题标题】:Flutter Error: setState() or markNeedsBuild() called during buildFlutter 错误:在构建期间调用了 setState() 或 markNeedsBuild()
【发布时间】:2021-06-14 19:06:58
【问题描述】:

我从服务器获取数据并将数据存储在一个列表中,然后我将该数据的一部分传递给一个提供程序类,其他一些小部件正在侦听该提供程序类,但我不知道为什么当我运行应用程序它给了我上述错误,虽然应用程序运行但数据更改时 UI 不会更新。它似乎没有在听。但我确信我为此添加了通知侦听器方法。

这是我的代码: 提供者类:

        import 'package:flutter/material.dart';
         class EmpAndState with ChangeNotifier {
         final String empId;
         String stateId;
         EmpAndState({@required this.empId, this.stateId = "1"});
          }

         class EmpsAstates with ChangeNotifier {
         List<EmpAndState> _items = [];
         List<EmpAndState> get items {
         return [..._items];
         }

         void addAll(List<String> empIdes) {
        final eIds = empIdes.map((e) => EmpAndState(empId: e)).toList();
       _items = eIds;
       notifyListeners();
       }


// void addItem(String eId, String stId) {
//   _items.add(EmpAndState(empId: eId, stateId: stId));
//   notifyListeners();
// }

void updateItem(String eId, String stId) {
 _items[_items.indexWhere((es) => es.empId == eId)] =
    EmpAndState(empId: eId, stateId: stId);
  notifyListeners();
   }

 EmpAndState findById(String eId) {
 return _items.firstWhere((es) => es.empId == eId);
 }
 }

监听类:

    import 'package:Attendece/provider/empAstate.dart';
    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    import '../provider/employeeAtypes.dart';

    class ListTileRow extends StatelessWidget {
    static const IconData cancel_outlined =
    IconData(0xe0c9, fontFamily: 'MaterialIcons');

  Widget build(BuildContext context) {
  Employee emp = Provider.of<Employee>(context);
  final empAsta = Provider.of<EmpsAstates>(context);
  final eas = empAsta.findById(emp.employeeId);
 return Row(
  mainAxisSize: MainAxisSize.min,
  mainAxisAlignment: MainAxisAlignment.end,
  children: [
    IconButton(
        icon: eas.stateId == "1"
            ? Icon(Icons.check_circle, color: Color(0xff507ce0))
            : Icon(Icons.check_circle_outline),
        onPressed: () {
          if (eas.stateId != "1") {
            eas.stateId = "1";
          } else {
            eas.stateId = "2";
          }
          emp.notifyListeners();
          empAsta.updateItem(emp.employeeId, eas.stateId);
        }),
    IconButton(
        icon: eas.stateId == "2"
            ? Icon(Icons.clear, color: Colors.red)
            : Icon(Icons.clear),
        onPressed: () {
          if (eas.stateId != "2") {
            eas.stateId = "2";
            emp.notifyListeners();
            empAsta.updateItem(emp.employeeId, eas.stateId);
          } else {
            eas.stateId = "1";
            emp.notifyListeners();
            empAsta.updateItem(emp.employeeId, eas.stateId);
          }
        }),
    IconButton(
        icon: eas.stateId == "3"
            ? Icon(Icons.airline_seat_flat, color: Color(0xff507ce0))
            : Icon(Icons.airline_seat_flat),
        onPressed: () {
          if (eas.stateId != "3") {
            eas.stateId = "3";
            emp.notifyListeners();
            empAsta.updateItem(emp.employeeId, eas.stateId);
          } else {
            eas.stateId = "1";
            emp.notifyListeners();
            empAsta.updateItem(emp.employeeId, eas.stateId);
          }
        }),
    IconButton(
        icon: eas.stateId == "5"
            ? Icon(Icons.home, color: Color(0xff507ce0))
            : Icon(Icons.home),
        onPressed: () {
          if (eas.stateId != "5") {
            eas.stateId = "5";
            emp.notifyListeners();
            empAsta.updateItem(emp.employeeId, eas.stateId);
          } else {
            eas.stateId = "1";
            emp.notifyListeners();
            empAsta.updateItem(emp.employeeId, eas.stateId);
          }
        }),
  ],
);
}
}

【问题讨论】:

    标签: flutter state-management


    【解决方案1】:

    你需要在你的应用入口点初始化provider

    provider 对 InheritedWidget 的封装,使它们更易于使用和更可重用。

    在初始化之前你不能访问它

     Widget build(BuildContext context) {
        return MultiProvider(
          providers: [
          ChangeNotifierProvider<MyProvider>(
                create: (context) => MyProvider()),
           ], child: MaterialApp(....
    

    还将 with ChangeNotifier 更改为extends ChangeNotifier

    【讨论】:

    • 我以前做过,我的主班也是这样。
    • 不要使用final for provider use var,尝试运行flutter clean然后重新运行app
    • 控制台显示提供程序类中的错误,我在 addAll() 方法中调用了 notifyListeners()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2021-04-19
    • 2019-02-19
    • 2020-12-12
    • 2020-12-28
    • 2021-01-06
    相关资源
    最近更新 更多