提供者值来自父小部件。我可以在构建上下文下使用提供程序值。但是,我需要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) …Run Code Online (Sandbox Code Playgroud)