简体   繁体   English

如何将列表作为参数发送到Flutter中的Firebase函数?

[英]How to send a list as a parameter to Firebase Functions in Flutter?

I would like to know, how to send or post a list (of maps) using the Cloud Functions Plugin for Flutter . 我想知道如何使用FlutterCloud Functions插件发送或发布地图列表。

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: 我正在尝试将此列表作为参数发送或发布到Flutter的Cloud Functions,如下所示:

   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 将复杂数据结构序列化/反序列化为Json对象的简单方法是使用package

marshalling . marshalling

Prototype (using "yaml" format) 原型(使用“ yaml”格式)

json_objects.yaml json_objects.yaml

Customer:
  name: String
  lastname: String  
CustomerList:
  customerList: List<Customer>

Auto generate code (using the utility "yaml2podo") 自动生成代码(使用实用程序“ yaml2podo”)

pub global run marshalling:yaml2podo json_objects.yaml

json_objects.dart 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>;
  }
}

Use generated code (automatically serialize/derialize objects) 使用生成的代码(自动序列化/序列化对象)

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}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM