gpt4 book ai didi

list - 在 firestore 集合中保存从 JSON URL 获取的列表

转载 作者:行者123 更新时间:2023-12-03 13:31:02 25 4
gpt4 key购买 nike

我的小应用程序正在从 JSON 链接获取用户列表,然后将其存储在列表中,我想将此列表放入 usersCollection firestore 的集合引用

usersCollection

我的代码

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:yat_flutter_app/main.dart';
import 'usersList.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class MyApp extends StatefulWidget {

@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

CollectionReference usersCollection =
FirebaseFirestore.instance.collection('users');

Future<List<User>> getUsers() async {
var data = await http
.get("https://www.json-generator.com/api/json/get/bYKKPeXRcO?indent=2");
var jasonData = json.decode(data.body);
List<User> users = [];

for (var i in jasonData) {
User user = User(i["index"], i["about"], i["name"], i["picture"],
i["company"], i["email"]);
users.add(user);
}

return users;
}


@override
Widget build(BuildContext context) {

List<User> usersList = getUsers() as List<User>;


return Container(

child: Column(
children: [
FutureBuilder(
future: getUsers(),
builder: (BuildContext context, AsyncSnapshot asyncSnapshop) {
if (asyncSnapshop.hasData) {
return Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: asyncSnapshop.data.length,
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 5,
color: Colors.cyan[50],
child: ListTile(
trailing: Icon(Icons.share),
title: Text(asyncSnapshop.data[index].name, style: TextStyle(fontFamily: 'Tahoma',fontSize: 20,fontWeight: FontWeight.bold),),
leading: CircleAvatar(
backgroundImage: NetworkImage(
asyncSnapshop.data[index].picture +
asyncSnapshop.data[index].index.toString() +
".jpg"),
),
subtitle: Text(asyncSnapshop.data[index].email,style: TextStyle(fontFamily: 'Tahmoma',fontSize: 18),),
onTap: (){
Navigator.push(context, new MaterialPageRoute(builder: (context)=>
detailsPage(asyncSnapshop.data[index])
));
},

onLongPress: ()=>

Fluttertoast.showToast(
msg: asyncSnapshop.data[index].name,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.green[900],
textColor: Colors.white,
fontSize: 16.0
),

),
);
}),
);
} else {
return Text("Loading, please wait...");
}
},
),

ElevatedButton(
child: Text('Save data'),
onPressed: () => {

usersCollection.add(getUsers()); // here's I am trying to add the result of getUsers into usersCollection

}),
],

),
);
}
}

最佳答案

要将对象推送到 Firestore,您需要将 object 转换为 map
你可以将这个函数添加到你的类中:

  Map<String, dynamic> toMap() {
return {
'field1': value1,
'field2': value1,
};
}

要推送一个 List ,您需要将所有对象转换为映射,您可以通过以下方法完成:

  static List<Map> ConvertToMap({List myList }) {
List<Map> steps = [];
myList.forEach((var value) {
Map step = value.toMap();
steps.add(step);
});
return steps;
}

或者简单地,参见 how to convert List to Map

希望有用

关于list - 在 firestore 集合中保存从 JSON URL 获取的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66077526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com