Im trying to convert old code to new code syntax. I have a issue with RxList.
So I change postModel.assign(postDetail);
But In my news_detail page How I can access to value?
First of all you shouldn''t use postModel as a List as your API clearly returns a single post (NewsModel) by id and not a list of post (List of NewsModel). So using var postModel = <NewsModel>[].obs; is totally unnecessary in my opinion.
What you could do is:
final postModel = NewsModel().obs;
And then on API call:
postModel.value = postDetail;
And then on View:
Image.network(controller.postModel.value.imageUrl);
postModel is a List.
So you would need to access an item in that list, using an int index.
Something like this:
return Image.network(newsDetailController.postModel[0].imageUrl);
Related
Im new and my question may be stupid but
in class Location i have 2 var :
var latitude;
var longitude;
then :
Location({Key? key, this.latitude , this.longitude}) : super(key: key);
and after some works when I print them I get the value
print(widget.latitude);
print(widget.longitude);
there is no problem here BUT when I want to use these vars in another class like this :
var myLat = Location().latitude;
var myLong = Location().longitude;
the values are NULL
how can get the values the second class too?
When you type Location() - you create the new instance of this class. It isn't connected to the previous instance you were working on. To make this work you need to type
var myLat = location.latitude;
var myLong = location.longitude;
WHERE location is the SAME object you created previously. So you need to pass it somehow to the place where you're trying to access these fields.
as you know you have 2 ways:
if you want to navigate from first page to second page you can pass the Location args by the code in my answer to a question. and if you want to read a doc here is the link.
you can use Provider as a stateManagement. here is my answer to using provider. believe me that using stateManagement make your tasks easy .because sometimes you need to use datas from one page to 10 pages later. what you want to do?? do you want to pass datas to every pages?????
if you didn't understand what i mean, please write in comment down.
happy coding...
Moraghebe khodet bash ;)
I ran into a problem with my Flutter app. The error I get in news_service.dart is "The sample member 'fromJson' cannot be accessed using static access." error. Anyone know what this is about? I really don't understand... Thanks in advance!!
if (response.body.isNotEmpty) {
final responseJson = json.decode(response.body);
News news = News.fromJson(responseJson);
return news.articles;
}
return null;
}
}
Try to replace News.fromJson(responseJson); with News().fromJson(responseJson);
You should instantiate New class then call instance method fromJson this method is not static so can't be accessed directly.
final news = News();
//now call news.fromJson(responseJson)
The fromJson function needs to either be static, or a (factory) constructor. It is hard to give an exact working answer because you have not provided additional information that was asked from you, but the following sample fromJson implementation should work with little modification from your part:
News.fromJson(Map<String, dynamic> json)
: title = json['title'],
content = json['content'];
I am assuming your News data model has title and content fields, feel free to change it according to your exact data model.
I'm writing a type safe builder package to wrap Firestore primitives called Fireproof. I'm currently working on wrapping a Query.where with my own custom FireQuery.where that I want to be fully typesafe. The Firestore where has a function declaration of
Query where(
dynamic field, {
dynamic isEqualTo,
dynamic isNotEqualTo,
dynamic isLessThan,
dynamic isLessThanOrEqualTo,
dynamic isGreaterThan,
dynamic isGreaterThanOrEqualTo,
dynamic arrayContains,
List<dynamic>? arrayContainsAny,
List<dynamic>? whereIn,
List<dynamic>? whereNotIn,
bool? isNull,
}) {/*...*/}
but as I'm generating code around one specific model or document, I know all the possible fields and their types so I don't want anything to be dynamic. However, I can't think of a way to take advantage of that or what the function call might look like.
Here's an example considering I have a user document
class UserDoc {
final int age;
final String city;
UserDoc(this.age, this.city);
}
and a UserQuery:
/// Generated Code
class UserQuery implements FireQuery {
// ...
UserQuery where(
// I know the only fields possible are age and city therefore
// I want the comparators to be typed to whatever is the selected field.
//
// E.g. If the user somehow selects age, isEqualTo should be isEqualTo<String>
) {
// TODO
}
}
Any ideas how this could be done?
EDIT
I deprecated the package in favor of Firestore's native withConverters and came to the same conclusion that, without partial classes or a large breaking change to the api this wouldn't be possible
You first link isn't working. Did you delete the project?
The dynamic part of the code you are referencing to it's there to adapt to any query you want to do. So if the user selects age you just need the following:
CollectionReference colref = Firestore.instance
.collection("usersCollection");
Query nameQuery = colref.where('age', isEqualTo: 'TheAgeYouWant');
and it will automatically get set to the value of Age. The point of using FlutterFire code is not having to create your own custom query and I don't think it really generates any benefits or advantages.
I have data in this order :-
m={ 1:[54,23,98],
9:[8,4,2]
}
I'm trying to achieve something like this using the following code but it's not working. Where am I going wrong? Is there another way of storing and accessing this type of data in dart if my method is completely wrong?
Map<int,List<int>> m;
m[0]=[];
m[0].add(5);
You have to initialise it with empty map first
Map<int, List<int>> m={}; //<- initialise it here
m[0] = [];
m[0].add(5);
print(m); // prints {0: [5]}
When I write like this
List.<Dictionary.<String, System.Object>>,
the ide tells me
Assets/Scripts/yhj/Model/PrintItem.js(23,71): BCE0044: expecting >, found '>>.
How can I resolve it?
Why would you make a list of dictionaries? Can't you just use the dictionary as the list with the key, value input instead of this? If you wanted to use it like this I would either define an object to be the <String, System.Object> and inserting that as the value and just leave the key as the number.
Or making a list of objects where the object is the <String, System.Object>
#ILiveForVR makes me think about this.thanks.
I solved it, but I'm not sure if the solution is best.
I do it like this:
var list = List.<System.Object>;
for(var i=0;i<list.Count;i++){
var data = list[i] as Dictionary<String, System.Object>;
}