Is there a way to solve the BuildContext problem? - flutter

I'm getting The argument type BuildContext Function() can't be assigned to the parameter type BuildContext.

You can't send context to a function when it isn't even defined in the initstate function. Here you are trying to use provider outside the Build Function which is a no no. What you can do as an alternative is to wrap the function with a Future to give it a valid context :
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
myString = Provider.of<AppData>(context, listen: false).testString;
});

I solved this by removing the flutter whole folder where it is saved and than remove the Environmental Values and remove path and add new download new Flutter SDK and than again update the path in Environmental Values

Related

How to change state value without rebuilding the widget in Flutter using riverpod?

I am using riverpod package for state management in a Flutter app and I have a search sort provider as follows:
final searchSortStateProvider = StateProvider<SearchSort?>((ref) { return SearchSort.relevance; });
But when the user searches again I want to reset the state of the search sort provider using the following code:
ref.read(searchSortStateProvider.notifier).state = SearchSort.relevance;
It throws:
"setState() or markNeedsBuild called during build"
Exception because I call it inside the init() method of the parent widget.
My question is how can I change the state of the provider without needing to rebuild the sort widget?
maybe you try ref.refresh or ref.invalidate/ref.invalidateSelf()?

using getx get.arguments in flutter breaks when doing a hot-reload

Using GetX with Flutter, I pass data between parent controller/screen to its child controller/screen using Get.arguments.
The issue is that when doing a hot-reload while working on a child Get.arguments, the parent gets re-instantiated with the arguments of the current controller/screen.
This issue is described here.
Is that a bug, or something that can't be avoided?
I use the Get.arguments in the controllers but still experiencing this issue...
Any suggestions?
since Get.arguments get it's value from the previous page, using it directly in your code will normally throw an error saying that it's null on hot reload/restart because it update the state
try saving Get.arguments first in a dynamic variable then use that varibale in your code
You need to pass the arguments while navigating from the 1 screen to second like:
onTap: () {
Get.toNamed(screen2, arguments: ['ApplicationDemo']);
},
On the 2nd screen/controller, store the arguments in a variable like:
var a = Get.arguments;
Then use it like:
Text(a[0].toString() + " " + a[1].toString()),
Alternatively, you can access it directly like:
Text(Get.arguments[0].toString() + " " + Get.arguments[1].toString());

Get overlay without context

I need a reference to the current OverlayState from a service where I don't have access to the current context. I've tried to use the "trick" of getting my BuildContext from a navigator key, but while this works for dialogs and navigation, does not seem to be valid for this use case. I've tried following approaches:
Overlay.of(navigatorKey.currentContext, rootOverlay: false);
Overlay.of(navigatorKey.currentState!.context, rootOverlay: false);
Overlay.of(navigatorKey.currentState!.overlay!.context, rootOverlay: false);
And all of them return null. Same piece of code, called from a widget (Overlay.of(context)) returns a valid OverlayState. Is there any way of achieving this?

The return type 'Future<Object?>' isn't a 'void', as required by the closure's context

I'm receiving this error. Can someone please help me :) ps: I'm new to flutter
enter image description here
You don't need the "return" statement because "onTap" it's a void function, so it doesn't require a "return", you code might work if you only delete this "return". Furthermore, when you call "pushNamed" from Navigator, it returns a Future, but you don't actually need to take the value from it.
It may help later if you take a peek on Flutter's documentation.
onTap(){
if(doc!["subCat"] == null)
Navigator.pushNamed(context, SellerBookForm.id);
}
There is no need to add return at the time of navigation. just call-
Navigator.pushName(context,"YourRouteName");

FlutterMap LateInitializationError State Management

I'm trying to make a Flutter app, using Riverpod for state management and FlutterMaps. I have set up notifiers for pages, and am also using a (State)NotifierProvider for the map page. I am building a search function, which goes to a search page which displays a list with search results. When one of these results (in a list) is clicked, I update the notifier of the map page. However, here comes the tricky part: I want to update the map according to the LatLng of the search result (center it).
My implementation of the Notifier contains:
late MapController? _mapController;
My map page is loaded in and gets the mapController from the Notifier: mapController: notifier.getMapController()
Using Riverpod state management, this all should work, however I get the following Error:
LateError (LateInitializationError: Field '_state#1225051772' has already been initialized.)
This Error is within the MapController itself (which is part of the FlutterMaps package), so I do not want to touch that. Does anyone have any idea how to tackle this?
lateerror is caused by nulling a variable that will fill before the application starts.
late MapController? _mapController;
this definition is wrong. Here, you are saying that this value will fill in soon. You also say that this value can be null.
late MapController _mapController;
if you do so this variable should not be null at runtime. if it is null you will get a lateerror error.
MapController? _mapController;
if you do it this way. This means that the value can be null. You should check when using.
like this
void sameFunction(){
if(_mapController!=null){
... your code
// here make sure this variable is not null. For this reason ! i put this
MapController anotherController=_mapController!;
}
}
or
void sameFunction(){
// Your code will not throw an error if this variable is null. Because specify that
//it can be null
mapController?.moveTo(...)
}
for more detailed information
you should remove the late keyword or you should intialise it in the initState
method.