gpt4 book ai didi

database - 类型 'DocumentSnapshot' 不是类型 'Map' 的子类型

转载 作者:行者123 更新时间:2023-12-05 01:31:36 24 4
gpt4 key购买 nike

这是我的个人资料页面
我也试过 snapshot.data()并尝试通过查看其他一些 StackOverflow 答案将其转换为 map 数据。请帮我解决这个问题。

错误在第 63 行。我不确定如何正确。实际上,在教程中,他们显示了 for from document 但我必须使用 FromMap 但我不确定如何使用它

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';

import 'package:justpoll/screens/profile/edit_profile.dart';
import 'package:justpoll/screens/profile/profile_nav_bar/settings.dart';
import 'package:justpoll/screens/profile/profile_nav_bar/help.dart';
import 'package:justpoll/Constants.dart';
import 'package:justpoll/widgets/progress.dart';
import 'package:justpoll/libmodels/user_model.dart';
import 'package:justpoll/libservices/authentication_service.dart';
import 'package:justpoll/screens/home_page/nav.dart';

class Profile2 extends StatefulWidget {
final String profileId;

Profile2({this.profileId});
@override
_Profile2State createState() => new _Profile2State();
}

final userRef = FirebaseFirestore.instance.collection("users");

class _Profile2State extends State<Profile2> {
buildProfileButton() {
return Text("profile Button");
}

Column buildCountColumn(String lable, int count) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
count.toString(),
style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold),
),
Container(
margin: EdgeInsets.only(top: 4.0),
child: Text(
lable,
style: TextStyle(
color: Colors.grey,
fontSize: 15.0,
fontWeight: FontWeight.w400,
),
),
)
],
);
}

buiildProfileHeader() {
return FutureBuilder(
future: userRef.doc(widget.profileId).get(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return circularProgress();
}

UserModel user = UserModel.fromMap(snapshot.data.data);

return Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
Row(
children: [
CircleAvatar(
radius: 40.0,
backgroundColor: Colors.grey,
backgroundImage: CachedNetworkImageProvider(user.photoUrl),
),
Expanded(
flex: 1,
child: Column(
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildCountColumn("posts", 0),
buildCountColumn("follower", 0),
buildCountColumn("following", 0),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildProfileButton(),
],
)
],
),
),
],
),
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(top: 12.0),
child: Text(
user.username,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.0,
),
),
),
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(top: 4.0),
child: Text(
user.name,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(top: 2.0),
child: Text(
user.bio,
),
),
],
),
);
},
);
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
iconTheme: IconThemeData(color: MyColors.black),
backgroundColor: Colors.white,
actions: [
Padding(
padding: const EdgeInsets.all(13.0),
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditProfile(),
),
);
},
child: Icon(
Icons.edit,
color: MyColors.black,
size: 26,
),
),
),
],
),
drawer: new Drawer(
child: new ListView(
children: <Widget>[
new ListTile(
leading: Icon(Icons.settings),
title: new Text('Settings'),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => SettingsScreen()),
);
},
),
new ListTile(
leading: Icon(Icons.bookmark),
title: new Text('Bookmarks'),
onTap: () {},
),
new ListTile(
leading: Icon(Icons.drafts),
title: new Text('Drafts'),
onTap: () {},
),
new ListTile(
leading: Icon(Icons.archive),
title: new Text('Archive'),
onTap: () {},
),
new ListTile(
leading: Icon(Icons.help),
title: new Text('help'),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => HelpPage()),
);
},
),
],
)),
body: ListView(
children: <Widget>[
buiildProfileHeader(),
],
),
);
}
}

这是我的用户模块

class UserModel {
String email;
String uid;
String username;
String name;
DateTime timestamp;
bool isPrivate;
String photoUrl;
String gender;
String bio;
String birthday;
Map followers;
Map following;

UserModel({
this.email,
this.uid,
this.username,
this.timestamp,
this.name,
this.photoUrl,
this.isPrivate,
this.gender,
this.bio,
this.birthday,
this.followers,
this.following,
});

Map toMap(UserModel user) {
var data = Map<String, dynamic>();

data["uid"] = user.uid;
data["username"] = user.username;
data["email"] = user.email;
data['name'] = user.name;
data['photoUrl'] = user.photoUrl;
data["timestamp"] = user.timestamp;
data["isPrivate"] = user.isPrivate;
data["gender"] = user.gender;
data["bio"] = user.bio;
data["birthday"] = user.birthday;
data["followers"] = user.followers;
data["following"] = user.following;
return data;
}

UserModel.fromMap(Map<String, dynamic> mapData) {
this.uid = mapData["uid"];
this.username = mapData["username"];
this.email = mapData["email"];
this.name = mapData["name"];
this.isPrivate = mapData["isPrivate"];
this.gender = mapData["gender"];
this.photoUrl = mapData["photoUrl"];
this.bio = mapData["bio"];
this.birthday = mapData["birthday"];
this.followers = mapData["followers"];
this.following = mapData["following"];
}
}

最佳答案

试试这个

var DocData = snapshot.data as DocumentSnapshot;

这为您提供了可以访问的数据

DocData['fieldname'];

你可以像这样使用字段

关于database - 类型 'DocumentSnapshot' 不是类型 'Map<String, dynamic>' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66074484/

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