class Resistencia100{
int id;
double r_pos1;
double r_pos2;
double r_pos3;
double r_pos4;
double r_pos5;
Resistencia100({
this.id, this.r_pos1, this.r_pos2, this.r_pos3, this.r_pos4,
this.r_pos5
});
Map<String, dynamic> toMap() => {
"id": id,
"r_pos1": r_pos1,
"r_pos2": r_pos2,
"r_pos3": r_pos3,
"r_pos4": r_pos4,
"r_pos5": r_pos5,
};
factory Resistencia100.fromMap(Map<String, dynamic> json) => new Resistencia100(
id: json["id"],
r_pos1: json["r_pos1"],
r_pos2: json["r_pos2"],
r_pos3: json["r_pos3"],
r_pos4: json["r_pos4"],
r_pos5: json["r_pos5"],
);
}
This is my Model class Resistencia100, Now we will see how I request the data through my get method
Future<List<Resistencia100>> getAllResistencia100() async {
final db = await database;
var response = await db.query("Resistencia100");
List<Resistencia100> list = response.map((c) => Resistencia100.fromMap(c)).toList();
print("Cantidad ID: "+list[0].id.toString());
print("Cantidad r_pos1: "+list[0].r_pos1.toString());
print("Cantidad r_pos2: "+list[0].r_pos2.toString());
print("Cantidad r_pos3: "+list[0].r_pos3.toString());
print("Cantidad r_pos4: "+list[0].r_pos4.toString());
print("Cantidad r_pos5: "+list[0].r_pos5.toString());
return list;
}
The information is coming correctly to the method, now I try to extract that information and the error is coming.
List <Resistencia100> resistencia100 = new List<Resistencia100>();
Future<List<Resistencia100>> getResistencia100() async {
await ClientDatabaseProvider.db.getAllResistencia100();
}
void validate() async {
resistencia100 = await getResistencia100();
print("RESISTENCIA ID: "+resistencia100[0].id.toString());
}
The truth is that I don't understand the reason for the error very well, I hope you can understand, I will leave the textual error in the following lines, this is generated in the "print".
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: [](0)
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 _ConfigConcretoState.validate (package:entremuros/vistas/configconcreto.dart:282:44)
Your method getResistencia100() is not returning anything. So at validate() your variable resistencia100 is transforming into a null after await the getResistencia100()
A solution is change the getResistencia100(), adding a return statement
Future<List<Resistencia100>> getResistencia100() async {
return await ClientDatabaseProvider.db.getAllResistencia100();
}
Related
I'm learning Dart (from a Java developer history), I'm working in a simple message application on Flutter.
The unique problem that I have is when I try to include a new Message in the chat's history. I am updating de array of messages in the app (Dart) and sending the entire object (Complex object with the messages array inside) to update via FirebaseFirestore to Firebase.
But I'm getting this error
I/flutter (16604): Invalid argument: Instance of 'Message'
This is my code
Adding a new message to the actual array
Message newMessage = Message('Test text',DateTime.now(), 'From me', 'For U' conversation.id);
messagesQueue.add(newMessage);//add the message to the actual array
conversation.messages = messagesQueue;
updateConversation(conversation);
Conversation object makes this transformation to send to Firebase
class Conversation {
String id;
String owner;
String destinationName;
String destination;
List<Message> messages;
String lastMessageDate;
....
//Transformacion para Firebase
Map<String, dynamic> toMap(Conversation conversation) {
return <String, dynamic>{
//'id': conversation.id,
'owner': conversation.owner,
'destinationName': conversation.destinationName,
'destination': conversation.destination,
//'messages': conversation.messages,
'messages' : List<dynamic>.from(conversation.messages.map((x) => x.toMap())),
'lastMessageDate': conversation.lastMessageDate
};
if I delete this line 'messages' : List<dynamic>.from(conversation.messages.map((x) => x.toMap())), the update works fine (no update messages obvious)
Message.dart code
class Message {
String text;
DateTime sendedTime;
String from;
String to;
String conversationId;
Message(
this.text,
this.sendedTime,
this.from,
this.to,
this.conversationId,
);
Map<String, dynamic> toMap() => {
"text": text,
"from": from,
"to": to,
"sendedTime": sendedTime.toIso8601String(),
"conversationId": conversationId,
};
}
The update method
Future<bool> updateConversation(Conversation conversation) async {
try {
await db.collection('conversations').doc(conversation.id).update(toMap(conversation));
return true;
} catch (e) {
print(e);
return false;
}
}
What is wrong with the List messages transformation?
UPDATE
I've added this line (var jsonMessages = conversation.messages.map((e) => e.toMap()).toList();) and the update works, but now I'm getting this error
_Error type 'InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Message'
Future<bool> updateConversation(String id, Conversation conversation) async {
try {
var jsonMessages = conversation.messages.map((e) => e.toMap()).toList();
//await db.collection('conversations').doc(id).update(toMap(conversation));
await db
.collection('conversations')
.doc(id)
.update({'messages': jsonMessages});
return true;
} catch (e) {
print(e);
return false;
}
}
Solved
Transformation from Firestore
Conversation.fromFirestore(DocumentSnapshot doc)
: id = doc.id,
owner = doc.get("owner"),
destinationName = doc.get("destinationName"),
destination = doc.get("destination"),
messages = List<Message>.from(
doc.get("messages").map((e) => Message.fromMap(e))),// this is the trick!
lastMessageDate = doc.get("lastMessageDate");
this is the code :
DatabaseReference referenceData=FirebaseDatabase.instance.ref().child('Jobs');
referenceData.get().then((DataSnapshot dataSnapshot) {jobs.clear();
var keys=dataSnapshot.value.keys;
var values = dataSnapshot.value;
for(var key in keys){
Jobs jobs = Jobs(values[key]['JobName'],values[key]['JobDetails']);
}});
and here are the problems :
The property 'keys' can't be unconditionally accessed because the receiver can be 'null'.
The method '[]' can't be unconditionally invoked because the receiver can be 'null'.
The method '[]' can't be unconditionally invoked because the receiver can be 'null'.
I have tried using this code :
DatabaseReference referenceData=FirebaseDatabase.instance.ref().child('Jobs');
referenceData.get().then((DataSnapshot dataSnapshot) {jobs.clear();
var keys=(dataSnapshot.value! as Map<String, dynamic>).keys;
var values = dataSnapshot.value;
for(var key in keys){
Jobs jobs = new Jobs((values! as Map<String, dynamic>)[key]['JobName'],(values! as Map<String, dynamic>)[key]['JobDetails']);
}});
the errors are gone but when I try to run the app I have this message :
Invalid depfile: C:\Users\AhXoX\StudioProjects\daimond.dart_tool\flutter_build\b5bec47ddb82374feed8ecd5e7242ad4\kernel_snapshot.d
Invalid depfile: C:\Users\AhXoX\StudioProjects\daimond.dart_tool\flutter_build\b5bec47ddb82374feed8ecd5e7242ad4\kernel_snapshot.d
lib/main.dart:4:8: Error: Not found: 'dart:html'
import 'dart:html';
^
lib/main.dart:201:79: Warning: Operand of null-aware operation '!' has type 'Object' which excludes null.
'Object' is from 'dart:core'.
Jobs jobs = new Jobs((values! as Map<String, dynamic>)[key]['JobName'],(values! as Map<String, dynamic>)[key]['JobDetails']);
^
I need help, please
You are trying to get data from firestore i mentioned the work i have done. Make fromJson factory method in Job class that takes Map<String,dynamic> as paramter just like that i am fetching user from firestore.
factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
uId: json["uID"],
name: json["name"],
image: json["image"],
number: json["number"],
status: json["status"],
typing: json["typing"],
online: json["online"],
);
now we will get all user from firestore.
Future<List<UserModel>> getAppContacts() async {
try {
final data = await FirebaseFirestore.instance.collection("users").get();
return data.docs.map((e) => UserModel.fromJson(e.data())).toList();
} on Exception catch (e) {
throw Exception(e.toString());
}
}
In your method i think you are passing extra [key] try to remove it that might be work.
I'm trying to get a specific field called "specie" from a document in a Firebase collection. I am trying as follows but I have an error of type 'Future ' is not a subtype of type 'String'. What am I doing wrong?
Repository method:
getSpecie(String petId) {
Future<DocumentSnapshot> snapshot = petCollection.document(petId).get();
return snapshot.then((value) => Pet.fromSnapshot(value).specie);
}
Entity method:
factory Pet.fromSnapshot(DocumentSnapshot snapshot) {
Pet newPet = Pet.fromJson(snapshot.data);
newPet.reference = snapshot.reference;
return newPet;
}
factory Pet.fromJson(Map<String, dynamic> json) => _PetFromJson(json);
Pet _PetFromJson(Map<String, dynamic> json) {
return Pet(json['name'] as String,
specie: json['specie'] as String);
}
I found a solution. No needed fromJson() method, I only changed the repository method:
Future<String> getSpecie(String petId) async {
DocumentReference documentReference = petCollection.document(petId);
String specie;
await documentReference.get().then((snapshot) {
specie = snapshot.data['specie'].toString();
});
return specie;
}
Try this..
getSpecie(String petId) async{
Future<DocumentSnapshot> snapshot = await petCollection.document(petId).get();
return snapshot.then((value) => Pet.fromSnapshot(value).specie);
}
This is how I learned to get documents from firestore
https://medium.com/#yasassandeepa007/how-to-get-sub-collection-data-from-firebase-with-flutter-fe1bda8456ca
In my flutter application, I AM PREPARING data to be sent via a REST call. Below is my code.
Future<void> saveOrderItemList(List<OrderItem> orderItems) async {
int responsetag = 0;
try {
List jsonList = OrderItem.encondeToJson(orderItems);
await http.post(_navLinks.saveOrderItems(),
body: jsonList,
headers: {
"Accept": "application/json",
"content-type": "application/json"
}).then((http.Response response) {
final int statusCode = response.statusCode;
print("RESPONSE: " + response.body);
print("STATUS CODE: " + statusCode.toString());
if (statusCode < 200 || statusCode > 400 || response.body == null) {
throw new Exception("Error while fetching data");
} else {
responsetag = int.parse(response.body);
}
});
return responsetag;
} catch (error) {
throw (error);
}
}
Below code shows the OrderItem model class
part 'order_item.g.dart';
/// An annotation for the code generator to know that this class needs the
/// JSON serialization logic to be generated.
#JsonSerializable()
class OrderItem {
int idorderItem;
FreshProducts freshProducts;
Order order;
ProductSize productSize;
double orderItemExpectedPricePerKg;
double orderItemQuantity;
int dateCreated;
int lastUpdated;
OrderItem(
{this.idorderItem,
this.freshProducts,
this.order,
this.productSize,
this.orderItemExpectedPricePerKg,
this.orderItemQuantity,
this.dateCreated,
this.lastUpdated});
/// A necessary factory constructor for creating a new instance
/// from a map. Pass the map to the generated `_$OrderItemFromJson()` constructor.
/// The constructor is named after the source class, in this case User.
factory OrderItem.fromJson(Map<String, dynamic> json) =>
_$OrderItemFromJson(json);
/// `toJson` is the convention for a class to declare support for serialization
/// to JSON. The implementation simply calls the private, generated
/// helper method `_$OrderItemToJson`.
Map<String, dynamic> toJson() => _$OrderItemToJson(this);
static List encondeToJson(List<OrderItem> list) {
List jsonList = List();
list.map((item) => jsonList.add(item.toJson())).toList();
return jsonList;
}
}
Below code shows the JSOB Serialization generated for the model class
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'order_item.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
OrderItem _$OrderItemFromJson(Map<String, dynamic> json) {
return OrderItem(
idorderItem: json['idorderItem'] as int,
freshProducts: json['freshProducts'] == null
? null
: FreshProducts.fromJson(json['freshProducts'] as Map<String, dynamic>),
order: json['order'] == null
? null
: Order.fromJson(json['order'] as Map<String, dynamic>),
productSize: json['productSize'] == null
? null
: ProductSize.fromJson(json['productSize'] as Map<String, dynamic>),
orderItemExpectedPricePerKg:
(json['orderItemExpectedPricePerKg'] as num)?.toDouble(),
orderItemQuantity: (json['orderItemQuantity'] as num)?.toDouble(),
dateCreated: json['dateCreated'] as int,
lastUpdated: json['lastUpdated'] as int,
);
}
Map<String, dynamic> _$OrderItemToJson(OrderItem instance) => <String, dynamic>{
'idorderItem': instance.idorderItem,
'freshProducts': instance.freshProducts,
'order': instance.order,
'productSize': instance.productSize,
'orderItemExpectedPricePerKg': instance.orderItemExpectedPricePerKg,
'orderItemQuantity': instance.orderItemQuantity,
'dateCreated': instance.dateCreated,
'lastUpdated': instance.lastUpdated,
};
However when I run my saveOrderItemList function, I get the following error.
I/flutter (23028): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'int' in type cast
Why is this happening?
I'm unable to find solutions from the previously available question, I have cast json string to map
Below is my API calling method.
Future<EventResponse> fetchEvent( ) async { // here i change Future type
String url='http://xxxxxxxxxxxx.tk/api/userapp/event/lists';
var headers = new Map<String, String>();//here i defined Map type
headers['Auth-Key'] = 'OCDOC#2018';
headers['End-Client'] = 'OCDOC';
var body = new Map<String, String>();//here i defined Map type
headers['schedule'] = 'present';
http.Response res = await http.post(url,headers: headers, body: body);
final parsed=json.decode(res.body);
var myMap = Map<String, dynamic>.from(parsed);
EventResponse eventResponse = EventResponse.convertEventResponse(myMap);
return eventResponse;
}
this is my convertEventResponse methode
factory EventResponse.convertEventResponse(Map<String, dynamic> json) {
List<dynamic> events = json['eventList'];
List<Event> eventList = events.map((e) => Event.convertEvent(e)).toList(); //here i changed by #Richard Heap answer
return EventResponse(
error: json['error'],
status: json['status'],
deliveryCharges: json['deliveryCharge'],
imageBaseUrl: json['image_base_url'],
imageLogoUrl: json['image_logo_url'],
eventList: eventList,
);
}
The error i'm getting.
InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments.
Use instead
.cast<String,dynamic>();
See also https://api.dartlang.org/stable/2.0.0/dart-core/Map/cast.html
Usually it's better to use Map<String,String>.from(oldMap) instead of cast<...>(...)