我正在从服务器获取数据并将数据存储在一个列表中,然后我将该数据的一部分传递给一个提供程序类,其他一些小部件正在侦听该提供程序类,但我不知道为什么当我运行应用程序它给了我上面的错误,虽然应用程序运行但用户界面不会在数据更改时更新。它似乎没有在听那个。但我确定我为此添加了通知侦听器方法。
这是我的代码: 提供者类:
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);
}
}),
],
);
}
}
答案 0 :(得分:0)
您需要在应用程序入口点中初始化提供程序
<块引用>provider InheritedWidget 的包装器,使它们更易于使用和可重用。
在初始化之前你不能访问它
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<MyProvider>(
create: (context) => MyProvider()),
], child: MaterialApp(....
还将 with ChangeNotifier
更改为 extends ChangeNotifier