0

I have this code

 @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('All users'), ), body: StreamBuilder<List<User>>( stream: readUsers(), builder: (context, snapshot) { if (snapshot.hasError) { return const Text('error fetching data'); } else if (snapshot.hasData) { if (snapshot.data!.isEmpty) { // return const Text('no data to fect'); return Container( padding: const EdgeInsets.all(10.0), child: const Text('no data'), ); } else { final users = snapshot.data!; return ListView( children: users.map(buildUser).toList(), ); } } else { return const Center(child: CircularProgressIndicator()); } }, ), } 

Then at this point

 return ListView( children: users.map(buildUser).toList(), ); 

I want to return data from another widget outside buildContext widget but the issue here is that I don't know how to pass the 'context' in the users.map(buildUser).toList() unorder to eliminate the error in the image below.

enter image description here

2
  • Where does 'buildUser' method locate? If you want to use helper method way, just pass the BuildContext as parameter of 'buildUser' method. Commented Aug 14, 2022 at 4:16
  • tried already but reported error at users.map(buildUser).toList(), Commented Aug 14, 2022 at 11:16

3 Answers 3

4

Create a class like bellow

import 'package:flutter/material.dart'; class GlobalContextService { static GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); } 

now assign this key to the MaterialApp in main.dart just like bellow

return MaterialApp( navigatorKey: GlobalContextService.navigatorKey, // set property ); 

Now you can access the context any where you want by using the following line of code

GlobalContextService.navigatorKey.currentContext 
Sign up to request clarification or add additional context in comments.

6 Comments

not understood, elaborate please
its the method if you want to get context anywhere in the app without passing it
can you give me sample with main.dart
from your code the class GlobalContextService is not called anywhere in your code
oh check it now
|
0

try this: Widget buildUser(User user, BuildContext context) =>

8 Comments

doing that I got this error The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'
I got the error here users.map(buildUser).toList(),
using code such like this Widget testWidget(String x, BuildContext context) => Container( width: 100, height: 100, color: Colors.red, child: GestureDetector( onTap: () { Navigator.of(context).pushNamed(CreateMatchPage.routeName); }, ), ); works fine for me i used testWidget inside my widget tree and passed the context to it and it works well without any problem you can provide us with more code examples to help you
did you try calling the function like this: users.map(buildUser).toList(),
How do I create a widget for this ** users.map(buildMember).toList()**
|
0

Recommended approach ->User helper widget instead of helper method.

or you can pass context as parameter to method

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.