Can't access model class in second page in Flutter - flutter

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.

Related

flutter: objects fields becoming null on creation in Isar inspector

I really liked Hive and recently i decided to try Isar as a local DB for my App ,However i'm facing a problem when trying to create an object into a collection using the Isar inspector as the fields are becoming null after creating the object .
User Class
import 'package:isar/isar.dart';
part 'user_model.g.dart';
#collection
class User {
Id id = Isar.autoIncrement; // you can also use id = null to auto increment
String? name;
int? age;
}
Here is the class that i'm generating the collection from
Creating an object from Isar inspector
and here is a GIF to better discribe the problem
fields are null on creation
any help or info is appreciated thank you
Simply put the text between double quotes on the inspector so instead of simple text be like that "simple text"

How set and use global variable in flutter app

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

How to properly add user data using Bloc && How to properly use Google Places API

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!

Apache Isis: How to implement your custom submit form or page properly?

I'm new at Apache Isis and I'm stuck.
I want to create my own submit form with editable parameters for search some entities and a grid with search results below.
Firstly, I created #DomainObject(nature=Nature.VIEW_MODEL) with search results collection, parameters for search and #Action for search.
After deeper research, I found out strict implementations for actions (For exapmle ActionParametersFormPanel). Can I use #Action and edit #DomainObject properties(my search parameters for action) without prompts?
Can I implement it by layout.xml?
Then I tried to change a component as described here: 6.2 Replacing page elements, but I was confused which ComponentType and IModel should I use, maybe ComponentType.PARAMETERS and ActionModel or implement my own IModel for my case.
Should I implement my own Wicket page for search and register it by PageClassList interface, as described here: 6.3 Custom pages
As I understood I need to replace page class for one of PageType, but which one should I change?
So, the question is how to implement such issues properly? Which way should I choose?
Thank you!
===================== UPDATE ===================
I've implemented HomePageViewModel in this way:
#DomainObject(
nature = Nature.VIEW_MODEL,
objectType = "homepage.HomePageViewModel"
)
#Setter #Getter
public class HomePageViewModel {
private String id;
private String type;
public TranslatableString title() {
return TranslatableString.tr("My custom search");
}
public List<SimpleObject> getObjects() {
return simpleObjectRepository.listAll();
}
#Action
public HomePageViewModel search(
#ParameterLayout(named = "Id")
String id,
#ParameterLayout(named = "Type")
String type
){
setId(id);
setType(type);
// finding objects by entered parameters is not implemented yet
return this;
}
#javax.inject.Inject
SimpleObjectRepository simpleObjectRepository;
}
And it works in this way:
I want to implement a built-in-ViewModel action with parameters without any dialog windows, smth like this:
1) Is it possible to create smth like ActionParametersFormPanel based on ComponentType.PARAMETERS and ActionModel and use this component as #Action in my ViewModel?
2) Or I should use, as you said, ComponentType.COLLECTION_CONTENTS? As I inderstand my search result grid and my search input panel will be like ONE my stub component?
Thank you.
We have a JIRA ticket in our JIRA to implement a filterable/searchable component, but it hasn't yet made it to the top of the list for implementation.
As an alternative, you could have a view model that provides the parameters you want to filter on as properties, with a table underneath. (I see you asked another question here on SO re properties on view models, so perhaps you are moving in that direction also... I've answered that question).
If you do want to have a stab at implementing that ticket, then the ComponentTYpe to use is COLLECTION_CONTENTS. If you take a look at the isisaddons, eg for excel or gmap3 then it might help get you started.
======= UPDATE TO ANSWER (based on update made to query) ==========
I have some good news for you. v1.15.0-SNAPSHOT, which should be released in the couple of weeks, has support for "inline prompts". You should find these give a user experience very similar to what you are after, with no further work needed on your part.
To try it out, check out the current trunk, and then load the simpleapp (in examples/application/simpleapp). You should see that editing properties and invoking actions uses the new inline prompt style.
HTH
Dan

Grails save extend class

how to save class extend in grails?
example i have class user and administrator
class User {
String name
String password
}
class Administrator extends User {
String authoritySelected
}
example in class User i have save "user1",
and then i want to change user1 from class user to class administrator and update authoritySelected
def update(){
def user1 = User.get("user1")
user1.authoritySelected
user1.save(flush:true)
}
and get error :
No such property: authoritySelected for class:User
so, how to save authoritySelected in class User and change that to class Administrator? thanks.
Speaking about the syntax, the code you wrote has no sense. Speaking about design, neither.
May I suggest you to study a bit a OOP before attempting doing this kind of stuff? :)
But let's face the problem you submitted.
First suggestion: don't implement the security system for your application, there's a lot of stuff that can do it for you. One above all: Spring Security plugin.
Second: the code you wrote doesn't work because extending a class is a way to make another class 'son' of the parent. In you example, Administrator is a son of User.
def update(){
def user1 = User.get("user1") // I don't get how this should work, but I'll leave it like this in this example
user1.authoritySelected // you're trying to GET the value a property that doesn't exist in the User class, you should SET something here
user1.save(flush:true)
}
If you want your User to change role, the easiest think is to think at the role not as another class, instead it should be an attribute of the User, so you can change it. Once the instance of a class is created, you can't change it (probably this is not totally true, but you shouldn't).
OK, some code:
class User {
String name
String password
String authority // a property of the class you can change
}
def update(){
def user1 = User.get("user1")
user1.authority = 'Administrator' // change the property on the instance you retrieved
user1.save() // save the instance itself
}
This is still not a good design solution to me, I'm just trying to make you able to see what you're doing wrong.
When you say "and then i want to change user1 from class user to class administrator", what exactly are you trying to do?
You are trying to access a property of an object that doesn't exist in that object. Downcasting simply doesn't work like that. You should instantiate an object of type Administrator in order to save one of its properties after.
if you want to create a USER you have to create an USER's instance, for example:
User u = new User(name: "xpto", password: "xptopass").save(flush:true)
An Administrator is a USER too, but with one more data, the authoritySelected, so if the Administrator extends User, he have the same data like a User too.
Administrator a = new Administrator(name: "xpto", password: "xptopPass", authoritySelected: "ADMIN").save(flush:true)
Attention, Object.get(X) methods needs an ID (Long), "X" would be a Long value, not a String.
http://grails.org/doc/2.3.x/ref/Domain%20Classes/get.html