Flutter:如何在子小部件的构建上下文之外访问函数中的"提供程序"值?



提供程序值来自父小部件。我可以在构建上下文下使用提供程序值。但是,我需要getHomeCampaigns函数中的提供程序值。我试图定义局部变量,并在构建小部件后将它们分配给提供者值,但函数声称这些变量是在null上调用的。我猜它们是在提供者的构建上下文下设置之前使用的。

class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
// Variables to be set inside the function and then used by the last widget
User currentUser;
String country;
List<CampaignWidget> friendsCamps;
List<CampaignWidget> featuredCamps;
// Function that needs the provider values
getHomeCampaigns() async {
// Get all campaigns where their owner is in the list of friends for the current user
QuerySnapshot friendsSnapshot = await Firestore.instance
.collection('campaigns')
.where('user.uid', whereIn: currentUser.friends)
.where('attributes.active', isEqualTo: true)
.orderBy('created', descending: true)
.limit(20)
.getDocuments();
// Get featured campaigns documents in the current country
QuerySnapshot featuredSnapshot = await Firestore.instance
.collection('campaigns')
.where('attributes.featured', isEqualTo: true)
.where('funders.${currentUser.uid}', isEqualTo: false)
.where('country', isEqualTo: country)
.orderBy('user.rating', descending: true)
.limit(5)
.getDocuments();
// Make 2 lists of CampaignWidget out of the documents retrieved
List<CampaignWidget> campaigns = friendsSnapshot.documents
.map((doc) => CampaignWidget(campaign: Campaign.fromDocument(doc)))
.toList();
List<CampaignWidget> featured = featuredSnapshot.documents
.map((doc) => CampaignWidget(campaign: Campaign.fromDocument(doc)))
.toList();
setState(() {
// Set the featured and friends lists of CampaignWidget to the newly made lists
this.featuredCamps = featured;
this.friendsCamps = campaigns;
});
}
@override
void initState() {
super.initState();
getHomeCampaigns();
}
@override
Widget build(BuildContext context) {
this.currentUser = Provider.of<User>(context);
this.country = Provider.of<String>(context);
return Scaffold(
backgroundColor: Color(0xFFE8E8E8),
appBar: AppBar(
centerTitle: false,
title: Text("Home"),
actions: <Widget>[
/// Search users
IconButton(
icon: Icon(
Icons.search,
),
onPressed: () {},
),
],
),
body: RefreshIndicator(
onRefresh: () => getHomeCampaigns(),
child: // Some widget that uses the variables,
),
);
}
}

您可以从提供程序中读取变量,如下所示:getHomeCampaigns中的Provider.of(context, listen: false).yourVariable。您必须有listen: false才能工作,因为它不在小部件树中,所以它不能在yourVariable更改时更新——请参阅。如果这不起作用,那么在声明您的提供者时就会出现问题。

您应该看看Riverpod。它填写了提供者的注意事项,并为您提供了一系列新功能。您可以在没有上下文的情况下访问类中定义的"provider"值和函数。它还允许您拥有多个相同类型的"提供者"。

我从我的api作为一个继承的Widget转移到了一个提供者"Flutter完整参考文献";由于复杂性,建议避免使用inheritWidget。我从materialapp开始,将它封装在包含我的restful调用的api类中。我用计数器查看按钮是否已按下。现在,我可以访问materialapp树中所有对象中的api调用。

void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My app',
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: Provider<Api>(
create: (context) => Api(),
child: Home(title: 'my title')));
}
}
class Home extends StatefulWidget {
Home({Key key, @required this.title}) : super(key: key);
final String title;
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _counter = 0;
onLogin(BuildContext context) {
LoginView param = new LoginView("abc@com",
"xxx");
Provider.of<Api>(context, listen: false)
.addLogin(context, param)
.then((value) {
setState(() {
_counter++;
});
}).catchError((error) {
DialogCaller.showErrorDialog(context, 
error.toString()).then((value) {});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Example'),
),
body: Container(
child: Column(
children: [
TextButton.icon(
label: Text("abc"),
icon: Icon(Icons.web),
onPressed: () {
onLogin(context);
setState(() {
_counter++;
});
}),
Text(
'$_counter',
style: 
Theme.of(context).textTheme.headline4,
)
],
)));
}
}

最新更新