【问题标题】:Error when pulling data from firestore using streambuilder使用streambuilder从firestore中提取数据时出错
【发布时间】:2020-01-04 08:47:12
【问题描述】:

我正在尝试使用 Flutter 从我的 Firestore 数据库中提取数据,但出现以下错误:

“String”类型不是“ImageProvider”类型的子类型

我正在使用流生成器并已定义类,但它似乎无法识别我需要提取的图像。以下是我用于流构建器的所有代码。

firestore database

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:ui' as ui;

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Profile Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Profile'),
    );
  }
}
class Photo {
  final int photourl;
  final DocumentReference reference;

  Photo.fromMap(Map<String, dynamic> map, {this.reference})
      : photourl = map['photourl'];

  Photo.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final _width = MediaQuery.of(context).size.width;
    final _height = MediaQuery.of(context).size.height;
    return StreamBuilder<DocumentSnapshot>(
        stream: Firestore.instance
            .collection('users')
            .document('testuser')
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Stack(
              children: <Widget>[
                new Container(
                  color: Colors.blue,
                ),
                new Image.network(
                  snapshot.data['photourl'].toString(),
                  fit: BoxFit.fill,
                ),
                new BackdropFilter(
                    filter: new ui.ImageFilter.blur(
                      sigmaX: 6.0,
                      sigmaY: 6.0,
                    ),
                    child: new Container(
                      decoration: BoxDecoration(
                        color: Colors.blue.withOpacity(0.9),
                        borderRadius:         BorderRadius.all(Radius.circular(50.0)),
                      ),
                    )),
                new Scaffold(
                    appBar: new AppBar(
                      title: new Text(widget.title),
                      centerTitle: false,
                      elevation: 0.0,
                      backgroundColor: Colors.transparent,
                    ),
                    drawer: new Drawer(
                      child: new Container(),
                    ),
                    backgroundColor: Colors.transparent,
                    body: new Center(
                      child: new Column(
                        children: <Widget>[
                          new SizedBox(
                            height: _height / 12,
                          ),
                          new CircleAvatar(
                            radius: _width < _height ? _width / 4 : _height / 4,
                            backgroundImage: snapshot.data['photourl'],
                          ),
                          new SizedBox(
                            height: _height / 25.0,
                          ),
                          new Text(
                            snapshot.data['name'],
                            style: new TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: _width / 15,
                                color: Colors.white),
                          ),
                          new Padding(
                            padding: new EdgeInsets.only(
                                top: _height / 30,
                                left: _width / 8,
                                right: _width / 8),
                          ),
                          new Divider(
                            height: _height / 15,
                            color: Colors.white,
                          ),
                          new Row(
                            children: <Widget>[
                              rowCell(
                                  snapshot.data['totalquestions'], 'Answers'),
                              rowCell('£ 673826', 'Earned'),
                            ],
                          ),
                          new Divider(
                              height: _height / 15, color: Colors.white),
                        ],
                      ),
                    ))
              ],
            );
          } else {
            return CircularProgressIndicator();
          }
        });
  }

【问题讨论】:

  • 你能把剩下的StreamBuilder代码贴出来吗?
  • @KarimElghamry 当然,刚刚将所有代码添加到问题中

标签: firebase flutter google-cloud-firestore


【解决方案1】:

在您的 Column 小部件中,您包含了一个 CircleAvatar 并将错误的参数传递给了 backgroundImage 属性。 backgroundImage 采用ImageProvider 而不是照片网址的String

你应该改变这个

CircleAvatar(
    radius: _width < _height ? _width / 4 : _height / 4,
    backgroundImage: snapshot.data['photourl'],
),

到此

CircleAvatar(
    radius: _width < _height ? _width / 4 : _height / 4,
    backgroundImage: NetworkImage(snapshot.data['photourl']),
),

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 2023-01-01
    • 1970-01-01
    • 2021-02-20
    • 2021-03-04
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2022-07-20
    相关资源
    最近更新 更多