我正在尝试从Firebase访问特定文档的数据 我使用外部索引来指定哪个文档。 我正在使用具有字符串和动态列表的aniaml模型 我的firebase文档包含字符串和字符串列表 DatabaseService.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:hkScan/models/animal.dart';
class DatabaseService {
DatabaseService({this.index});
final index;
final CollectionReference animalsCollection =
Firestore.instance.collection('Animals');
Animal _animalFromSnapshot(QuerySnapshot snapshot) {
try {
return Animal(
type: snapshot.documents[index].data['type"'] ?? '',
feeding: snapshot.documents[index].data['feeding'] ?? '',
slaughter: snapshot.documents[index].data['slaughter'] ?? '',
breed: snapshot.documents[index].data['breed'] ?? '',
cuts: snapshot.documents[index].data['cuts'] ?? '',
grading: snapshot.documents[index].data['rating'] ?? '',
image: snapshot.documents[index].data['image'] ?? '',
);} catch(e){print(e.toString());return null;}
}
Stream<Animal> get animals {
return animalsCollection.snapshots().map<Animal>(_animalFromSnapshot);
}
}
动物模型:
class Animal {
final String type;
final String feeding;
final List<dynamic> slaughter;
final List<dynamic> breed;
final List<dynamic> grading;
final List<dynamic> cuts;
final List<dynamic> image;
Animal(
{this.type,
this.feeding,
this.slaughter,
this.breed,
this.grading,
this.cuts,
this.image});
}
这就是我使用StreamProvider将流从Firebase传递到我的应用的方式
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hkScan/models/animal.dart';
import 'package:hkScan/services/database.dart';
import 'package:hkScan/widgets/animalsGrid.dart';
import 'package:hkScan/widgets/productCard.dart';
import 'package:provider/provider.dart';
import 'models/style.dart';
class Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<Body> {
int isSelected = 0;
int dindex = 0;
@override
Widget build(BuildContext context) {
return StreamProvider<Animal>.value(
value: DatabaseService(index: dindex).animals,
child: Container(
color: Color(0xFF161D36),
child: Stack(
children: [
.
.
.
.
.
Positioned(
left: 21,
top: 355,
height: 290,
width: 260,
child: ListView.builder(
itemCount: 2,
itemBuilder: (context, index) {
final animal = Provider.of<Animal>(context);
return animal==null? Container():ProductCard(animal: animal,index: index,);
})),
.
.
.
.
.
产品卡:
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:hkScan/models/animal.dart';
import 'package:hkScan/models/style.dart';
class ProductCard extends StatelessWidget {
final Animal animal;
final index;
ProductCard({this.animal, this.index});
@override
Widget build(BuildContext context) {
return ClipPath(
clipper: RoundedDiagonalPathClipper(),
child: Stack(children: [
Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(25),
),
),
Positioned(
child: ClipRRect(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(25),
bottomRight: Radius.circular(25)),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 25.0, sigmaY: 25.0),
child: Container(
width: 260.0,
height: 71.0,
decoration: BoxDecoration(
color: Colors.grey[900].withOpacity(0.2),
boxShadow: [
BoxShadow(
color: Colors.black38,
blurRadius:
15.0, // has the effect of softening the shadow
spreadRadius:
10.0, // has the effect of extending the shadow
offset: Offset(
-13.0, // horizontal, move right 10
15.0, // vertical, move down 10
),
)
],
),
),
),
),
bottom: 0,
),
Positioned(
left: 20,
bottom: 15,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(animal.type,
style: myStyle((20), FontWeight.w400, Color(0xFFD1E2F4))),
Text(animal.grading[index],
style: myStyle((15), FontWeight.w400, Color(0xFFD1E2F4)))
],
)),
]),
);
}
}
FireBase: Firebase Console 但是我一直收到此错误:
I/flutter (11723): type 'String' is not a subtype of type 'List<dynamic>'
我该如何解决? 谢谢