On main.dart I get response from api username and set `String myUsername = 'stackoverflow';
How on all page use myUsername as global variable?
the answer above is right ,
however if you don't want to make a singleton class,
all you can do is, save it to shared preference, using the
shared_preferences
myPreference.setString("userName", Your user name)
and get it using
myPreference.getString("userName")
You can create a Singleton like this:
class SingletonTest{
String myUsername;
SingletonTest._private();
static SingletonTest _instance = SingletonTest._private();
static SingletonTest get instance => _instance;
}
After that, you can access it from anywhere in your code using:
var singl = SingletonTest.instance;
For more examples refer to:
How to build a Singleton in dart
Or you can use Provider Pattern, more info about it here:
Provider Pattern
Related
How could you store something like that even when the app is closed in Flutter:
Class myList {
String id;
List<Item> list;
}
Class Item{
//Many property’s
}
I thought maybe I could do that with "sqflite", a flutter dependency, but I have no Idea how I could store there this List<Item>. Do you have any idea?
Btw: I need multiple of these "myList" instances.
You can store it using sqflite,hive or shared preferences.Try to save it under flutter shared preferences. These are two methods that you can use.
Using shared preferences
1)First create a shared preference instance
SharedPreferences prefs = await SharedPreferences.getInstance();
Then save the relevant data type.here you need to put a object list so add the list and convert it to the String using jsonEncode.
Map<String,List<Item>> map={mylist.id: mylist.list};
prefs.setString("itemList", json.encode(map));
Then you can retrieve data like this
Map<String,List<Item>> map=json.decode(prefs.getString("itemList")).asMap();
2)Using Hive
First Create the hive databse.you can put any name here
var box = Hive.box('myBox');
Then add the object in to that database
var myList = myList()
..id = your id here
..list = add list here;
var name = box.add(myList);
You can get anywhere this data list.
print(box.getAt(0));
You can try hive package. It is a lightweight local storage passage and it is a good idea to use hive because it has better benchmarks for reading and writes. You can search for the tutorial for the hive. or just read the documentation.
I want to check in my app anywhere if the user is an admin or not. For that, I am first fetching the admins list from the server and set a global file that contains ValueNotifier<bool> admin to notify if we have set or change the value of admin after fetching the admin list. But problem is that admin.value setter will also be accessible to all files to set its value, but I want to restrict it such that the value of admin can only be changed in Networks class or Network files or any other specific files, while I want to listen on its changes anywhere possible. Can anyone help me?
You could make your ValueNotifier a private instance so it will only be accessible and editable in its current class then make a public getter for its listenable value which you will be able to call from outside.
Example
class Network{
// private variable only accessible inside the class
final ValueNotifier<bool> _isAdminNotifier = ValueNotifier<bool>(false);
// public getter accessible outside
ValueListenable<bool> get isAdmin => _isAdminNotifier;
}
Try the full example on DartPad.
Ok Got it, I can create a ValueListenable<bool> getter on ValueNotifier<bool> admin.
I am trying to access a model class in the second page of my app.
In the first which I have named Main Activity I am able to call the class and write an updated value to it. My class is as follows:
class UserDetails{
String userName = "";
String userSurname = "";
}
In MainActivity I am able to call the above and update the userName and userSurname after reading from Firestore and print in the console:
UserDetails details = UserDetails();
details.userName = userName;
details.userSurname = userSurname;
print(details.userName);
print(details.userSurname);
When I move to the second page and try to call the same first two lines shown below I cannot even autocomplete the details.userName.
UserDetails details = UserDetails();
details.userName = userName;
The package is imported on both MainActivity and the second page. any idea why I cannot get UserDetails in the second? I thought maybe it is not re-usable across pages but even if I remove it from MainActivity it still doesnt allow the usage on the second page.
Essentially what I am trying to do is use userName and userSurname as global variables.
if the class is imported in both of your classes.
Run the command flutter clean. That would solve your problem.
I hope this helps.
In my project I am calling the rest API which result gives a list of Map.
my App has 4 bottomtab options, in Dashboard I am hitting the API, and I am doing some operations on the data, but I need the result of that API should be available to all the tabs, else I have to hit again and store in that dart file,
Is there any way whrere I can Hit once and store it into a map and I can use that all over my project?
thanks!
Create a static List variable of your Data by Creating a new Class named Global
class Global
{
static List<MYDataClass> MyDataList = List();
}
to Add Data pass Object of MYDataClass
MyDataList.add(MYDataClass())
to Access it
Global.MyDataList[index].property;
Hope this Help...
I'm having difficulties in two areas:
1) When a user successfully logs into their account using phone auth in Firestore, I take them to an "Edit profile" page so they can add their profile information. I get an error saying that I can't add data to a null user class or add data to a class within a class.
I currently have my user class setup something like the following:
class User {
String points;
Name name;
User({this.points, this.name});
}
class Name {
String firstName;
String lastName;
Name({this.firstName, this.lastName})
}
As you can see, I have a class within a class and when I try to add a value, it says I can't. I've tried doing it like
_bloc.user.name.firstName = value
And I've tried like
Name newName = Name();
newName.first = value.
The second one seems to work but it doesn't seem right. I'm hoping you could help me understand how to properly approach adding data for new users when I have a class within a class.
The second issue is understanding how to properly use the Places API. I'm currently learning from the below repo, but it's outdated and there's a couple lines I can't seem to figure out how to change. I also can't seem to find an updated tutorial since the October app crashing update.
https://github.com/alfianlosari/flutter_placez
Thanks in advance for your help!