I would like to know, how to send or post a list (of maps) using the Cloud Functions Plugin for Flutter .
I have some maps like these:
var customer1 = {'name':'tom','lastname':'smith'};
var customer2 = {'name':'bob','lastname':'lee'};
var customer3 = {'name':'george','lastname':'collins'};
These maps later become a list:
var _customerList = [customer1, customer2, customer3];
I am trying to send or post this list as a parameter to Cloud Functions for Flutter like this:
var _data = {
'customerList': _customerList,
};
final HttpsCallable callable = CloudFunctions.instance
.getHttpsCallable(functionName: 'createCustomer')
..timeout = const Duration(seconds: 30);
final HttpsCallableResult result = await callable.call(_data);
However, I am getting this error:
I/flutter (11486): Invalid argument: Instance of ...
If I try to post a simple string everything works fine. Nevertheless, it seems that I am not posting the list of maps correctly.
I am using this version of the plugin:
dependencies:
cloud_functions: ^0.3.0+1
Thanks for your help.
Easy way to serialize/deserialize complex data structures to Json objects is to use package
marshalling
.
json_objects.yaml
Customer:
name: String
lastname: String
CustomerList:
customerList: List<Customer>
pub global run marshalling:yaml2podo json_objects.yaml
json_objects.dart
// Generated by tool.
import 'package:marshalling/json_serializer.dart';
final json = JsonSerializer()
..addType(() => Customer())
..addType(() => CustomerList())
..addIterableType<List<Customer>, Customer>(() => <Customer>[])
..addAccessor('customerList', (o) => o.customerList, (o, v) => o.customerList = v)
..addAccessor('lastname', (o) => o.lastname, (o, v) => o.lastname = v)
..addAccessor('name', (o) => o.name, (o, v) => o.name = v)
..addProperty<Customer, String>('name')
..addProperty<Customer, String>('lastname')
..addProperty<CustomerList, List<Customer>>('customerList');
class Customer {
String name;
String lastname;
Customer();
factory Customer.fromJson(Map map) {
return json.unmarshal<Customer>(map);
}
Map<String, dynamic> toJson() {
return json.marshal(this) as Map<String, dynamic>;
}
}
class CustomerList {
List<Customer> customerList;
CustomerList();
factory CustomerList.fromJson(Map map) {
return json.unmarshal<CustomerList>(map);
}
Map<String, dynamic> toJson() {
return json.marshal(this) as Map<String, dynamic>;
}
}
import 'json_objects.dart';
void main() {
var customers = <Customer>[];
var customer = Customer()
..name = 'tom'
..lastname = 'smith';
customers.add(customer);
customer = Customer()
..name = 'bob'
..lastname = 'lee';
customers.add(customer);
customer = Customer()
..name = 'george'
..lastname = 'lee';
customers.add(customer);
var customerList = CustomerList();
customerList.customerList = customers;
var data = customerList.toJson();
// final HttpsCallableResult result = await callable.call(data);
print(data);
}
Result:
{customerList: [{name: tom, lastname: smith}, {name: bob, lastname: lee}, {name: george, lastname: lee}]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.