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

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());

Related

Is there a way to solve the BuildContext problem?

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

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()?

How to delete an item from a list in Flutter using provider?

I have a list that I am trying to delete a specific item from.
I first tried:
Provider.of<WeekList>(context, listen: false).listOfWeeks.remove(widget.index);
That did not work.
After thinking about it I realized that the Provider is retrieving the list but not actually updating the provider.
So then I decided to use the provider to save retrieve the list and save it to a new list:
List<Week> myList =Provider.of<WeekList>(context, listen: false).listOfWeeks;
I then tried to delete an item at the current position:
myList.remove(widget.index);
I expected the myList to be shortened by one after that last line of code. But I put a print before and after it and they both still say the length is 6...
not sure why its not removign anything from myList. If it worked it should shorten it by one and I planned on then trying to update the provider.... But maybe I am not goign about this correctly.
in your last approach you are changing a "copy" of your list, what you need to do to achieve your goal is to make function inside your state that updates your list and notifies the listeners,
this function to be put inside your ChangeNotifier class:
void updateList(int index)
{
listOfWeeks.removeAt(widget.index);
notifyListeners();
}
and you can call it like:
Provider.of<WeekList>(context, listen: false).updateList(index);
If you're removing by index, you need to use removeAt.

How to cache a value returned by a Future?

For my Flutter app I'm trying to access a List of Notebooks, this list is parsed from a file by the Api. I would like to avoid reading/parsing the file every time I call my notebooks getter, hence I would like something like this:
if the file had already been parsed once, return the previously parsed List<Note> ;
else, read and parse the file, save the List<Note> and returns it.
I imagine something like this :
List<Notebook> get notebooks {
if (_notebooks != null) {
return _notebooks;
} else {
return await _api.getNotebooks();
}
}
This code is not correct because I would need to mark the getter async and return a Future, which I don't want to. Do you know any solution for this problem, like caching values ? I also want to avoid initializing this list in my app startup and only parse once the first time the value is needed.
you can create a singleton and you can store the data inside a property once you read it, from next time onwards you can use the same from the property.
You can refer this question to create a singleton
How do you build a Singleton in Dart?

Initialize my list from the backend when building for the first time

I have a list of Contact objects that I display within a ListView. What I'd like to do is to initialize my list from the backend when the page builds for the first time.
I get the data from the backend with the ScopedModel.
So what I tried to do is :
ScopedModelDescendant<Account>(
builder: (context, child, _account){
...
if(_account.profile.contacts != null)
_account.profile.contacts.forEach((contact){
Contacts contactToAdd = new Contacts(contact.name, contact.label, contact.phonenumber,contact.email);
contactToAdd.onTap = editContact(contactToAdd);
listOfContact.add(contactToAdd);
});
}
)
But the problem is, as the code is in the build method, every time I add a contact, everythings build because of the ScopedModel and this code is re-executed Making that every time I add one contact, the whole list is added again which duplicates and multiplies a lot of things.
Why don't I execute this code outside of the Build method, in a didChangeDependencies() for exemple ? Because I need the datas of the Scoped Model (account) to make my test.
So I have no idea how to proceed :/ Any idea ?
You can use the BloC pattern, it will retrieve the info only once.
https://medium.com/flutter-community/flutter-oneyearchallenge-scoped-model-vs-bloc-pattern-vs-states-rebuilder-23ba11813a4f