Flutter 未更新ChangeNotifierProvider的侦听器
我尝试了多个Flutter 未更新ChangeNotifierProvider的侦听器,flutter,dart,flutter-provider,Flutter,Dart,Flutter Provider,我尝试了多个ChangeNotifierProvider(final xyz=provider.of(context),Consumer)的实现,但无法在我的侦听器/消费者中接收更新的列表。它总是返回一个空列表 I/flutter ( 4410): [] 在我的闪屏中,我正在从providers/digital\u heromes.dart调用digitalheromes().getheromes()。它调用函数并打印cms中新英雄列表的长度,即1,因此我知道,\u英雄列表正在提供者类中更新
ChangeNotifierProvider
(final xyz=provider.of(context)
,Consumer
)的实现,但无法在我的侦听器/消费者中接收更新的列表。它总是返回一个空列表
I/flutter ( 4410): []
在我的闪屏中,我正在从providers/digital\u heromes.dart调用digitalheromes().getheromes()
。它调用函数并打印cms中新英雄列表的长度,即1,因此我知道,\u英雄
列表正在提供者类中更新
下面是现有的实现
型号/数码英雄.dart
class DigitalHero {
String name;
String title;
String title_extended;
String phone_number;
String email_address;
double location_lat;
double location_long;
String image;
String bio;
Map<String, dynamic> related_projects;
DigitalHero({
this.name,
this.title,
this.title_extended,
this.phone_number,
this.email_address,
this.location_lat,
this.location_long,
this.image,
this.bio,
this.related_projects,
});
static DigitalHero fromJson(Map<String, dynamic> json) {
return DigitalHero(
name: json['name'] as String,
title: json['title'] as String,
title_extended: json['title_extended'] as String,
phone_number: json['phone_number'] as String,
email_address: json['email_address'] as String,
location_lat: json['location_lat'] as double,
location_long: json['location_long'] as double,
image: json['image'] as String,
bio: json['bio'] as String,
related_projects: json['related_projects'] as Map<String, dynamic>,
);
}
Map<String, dynamic> toJson() => {
'name': name,
'title': title,
'title_extended': title_extended,
'phone_number': phone_number,
'email_address': email_address,
'location_lat': location_lat,
'location_long': location_long,
'image': image,
'bio': bio,
'related_projects': related_projects,
};
}
我终于弄明白了。我正在创造一个全新的数字英雄形象
await DigitalHeroes().getHeroes();
而不是像这样使用继承的实例
await Provider.of<DigitalHeroes>(context, listen: false).getHeroes();
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<DigitalHeroes>( //tried with multiprovider, no change
create: (ctx) => DigitalHeroes(),
child: MaterialApp(
title: 'xyz',
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: MyHomePage(title: 'Home'),
routes: {
Splash.routeName: (ctx) => Splash(),
Onboarding.routeName: (ctx) => Onboarding(),
Login.routeName: (ctx) => Login(),
BottomTabsScreen.routeName: (ctx) => BottomTabsScreen(),
Home.routeName: (ctx) => Home(),
DigitalHeroesScreen.routeName: (ctx) => DigitalHeroesScreen(),
},
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
Singleton.instance.screenSize = MediaQuery.of(context).size;
return Container(
child: Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Splash(),
),
);
}
}
void initData() async {
setState(() {
_loading = true;
});
// ignore: await_only_futures
await DigitalHeroes().getHeroes();
setState(() {
_loading = false;
});
if (!_loading && !_animating) {
Navigator.of(context).pushReplacementNamed(Onboarding.routeName);
}
}
await DigitalHeroes().getHeroes();
await Provider.of<DigitalHeroes>(context, listen: false).getHeroes();
class Heroes extends ChangeNotifier {
List _heroes;
void getData() async {
// Loading Assets
_heroes=heroes;
notifyListeners();
}
Heroes() {
getData();
}
}