颤振导航器从firebase推送数据
我们正在尝试从firebase获取数据,我们目前正在网格视图中呈现这些数据,但我希望能够在网格视图中单击该项并阅读更多有关它的信息 这就是我到目前为止的情况,也是我在VS中遇到的错误颤振导航器从firebase推送数据,firebase,flutter,dart,flutter-navigation,Firebase,Flutter,Dart,Flutter Navigation,我们正在尝试从firebase获取数据,我们目前正在网格视图中呈现这些数据,但我希望能够在网格视图中单击该项并阅读更多有关它的信息 这就是我到目前为止的情况,也是我在VS中遇到的错误 The argument type 'DocumentSnapshot' can't be assigned to the parameter type 'Merchant'. 主屏幕 import 'package:flutter/material.dart'; import 'package:flutter/
The argument type 'DocumentSnapshot' can't be assigned to the parameter type 'Merchant'.
主屏幕
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:paylaterapp/screens/GridMerchantDetails.dart';
Future<void> main() async {
final FirebaseApp app = await FirebaseApp.configure(
name: 'BNPL',
options: const FirebaseOptions(
googleAppID: '***',
gcmSenderID: '***',
apiKey: '***',
projectID: '***',
),
);
final Firestore firestore = Firestore(app: app);
await firestore.settings(timestampsInSnapshotsEnabled: true);
}
class HomeScreen extends StatelessWidget {
HomeScreen(this.firestore);
final Firestore firestore;
CollectionReference get merchants => firestore.collection("merchants");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stores'),
),
body: _gridView(),
);
}
Widget _gridView() {
return StreamBuilder<QuerySnapshot>(
stream: firestore.collection('merchants').orderBy('name', descending: false).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int merchantCount = snapshot.data.documents.length;
return GridView.builder(
scrollDirection: Axis.vertical,
itemCount: merchantCount,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemBuilder: (_, int index) {
final DocumentSnapshot document = snapshot.data.documents[index];
final dynamic logo = document['logo_url'], mainImage = document['main_image_url'];
return
CupertinoButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => GridMerchantDetails(document),
),
);
},
child: (
Container(
height: 300,
width: 300,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.grey,
image: DecorationImage(
image: new NetworkImage(mainImage != null ? mainImage.toString() : 'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
)
)
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Image.network(logo != null ? logo.toString() : 'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
width: 220,
fit: BoxFit.fill,
)
]
)
)
)
);
},
);
},
);
}
}
class GridMerchantDetails extends StatelessWidget {
final Merchant merchant;
GridMerchantDetails(this.merchant);
@override
Widget build(BuildContext context) {
return Scaffold(
primary: true,
appBar: AppBar(
title: Text(merchant.name),
),
backgroundColor: Colors.deepPurpleAccent,
body: ListView(
children: <Widget>[
HeaderBanner(this.merchant),
Container(
padding: const EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 20.0),
child: Text(
merchant.desc,
style: TextStyle(
fontSize: 13.0,
color: Colors.white,
),
),
),
],
),
);
}
}
导入“包装:颤振/材料.省道”;
进口“包装:颤振/cupertino.dart”;
导入“dart:async”;
导入“包:firebase_core/firebase_core.dart”;
导入“包:cloud_firestore/cloud_firestore.dart”;
导入“package:paylaterapp/screens/GridMerchantDetails.dart”;
Future main()异步{
final FirebaseApp app=等待FirebaseApp.configure(
名称:'BNPL',
选项:常量FirebaseOptions(
googleAppID:“***”,
gcmSenderID:“***”,
apiKey:“***”,
projectID:“***”,
),
);
最终Firestore Firestore=Firestore(应用程序:应用程序);
等待firestore.settings(timestampsInSnapshotsEnabled:true);
}
类主屏幕扩展无状态小部件{
主屏幕(这是firestore);
最终消防仓库消防仓库;
CollectionReference get merchants=>firestore.collection(“商家”);
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“存储区”),
),
正文:_gridView(),
);
}
控件_gridView(){
返回流生成器(
流:firestore.collection('merchants')。orderBy('name',降序:false)。快照(),
生成器:(BuildContext上下文,异步快照){
如果(!snapshot.hasData)返回常量文本('Loading…');
最终int merchantCount=snapshot.data.documents.length;
返回GridView.builder(
滚动方向:轴垂直,
itemCount:merchantCount,
gridDelegate:
新SliverGridDelegateWithFixedCrossAxisCount(
交叉轴计数:2),
itemBuilder:(\ux,int索引){
final DocumentSnapshot document=snapshot.data.documents[索引];
最终动态徽标=文档['logo_url'],mainImage=文档['main_image_url'];
返回
丘比特纽扣(
已按下:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>GridMerchantDetails(文档),
),
);
},
儿童:(
容器(
身高:300,
宽度:300,
装饰:盒子装饰(
边界半径:边界半径。圆形(10),
颜色:颜色。灰色,
图像:装饰图像(
image:newnetworkimage(mainImage!=null?mainImage.toString():'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
)
)
),
子:列(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
Image.network(logo!=null?logo.toString():'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
宽度:220,
fit:BoxFit.fill,
)
]
)
)
)
);
},
);
},
);
}
}
类GridMerchantDetails扩展了无状态小部件{
最终商户;
GridMerchantDetails(本商户);
@凌驾
小部件构建(构建上下文){
返回脚手架(
小学:对,
appBar:appBar(
标题:文本(商户名称),
),
背景颜色:颜色。深紫色,
正文:ListView(
儿童:[
校长班纳(这位商人),
容器(
填充:LTRB(10.0,0.0,10.0,20.0)中的常数边集,
子:文本(
merchant.desc,
样式:TextStyle(
字体大小:13.0,
颜色:颜色,白色,
),
),
),
],
),
);
}
}
Leme查看您的GridMerchantDetails(document)@Ggriffo添加了类尝试检查DocumentSnapshot是否符合Merchant的格式?