Flutter:从firebase检索数据

时间:2020-10-20 13:37:08

标签: firebase flutter dart google-cloud-firestore

在模拟器中,evrything还可以,但是在真实设备中崩溃了,我从列表磁贴中的botton调用了该类,这是我的错误this is the error screenshot in real device when i call this class 如果我在调用firebase集合时在集合中使用变量,有什么问题吗?

这是我的通话按钮:

onPressed: () {
             Navigator.push(context,
             MaterialPageRoute(builder: (context) => DetailPage(name)));
              },

这是我的代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class  DetailPage extends StatefulWidget {
  final String index;
  DetailPage(this.index);

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

class _DetailPageState extends State<DetailPage> {

  var filepath;
  var response;



  @override
  Widget build(BuildContext context) {
     return FutureBuilder(builder: (context, snapshot) {
      return Scaffold(
         appBar: AppBar(
            backgroundColor: Color(0xff0fd1c0),
            centerTitle: true,
            title: Text(
          '${widget.index}',
          textDirection: TextDirection.rtl,
        ),
      ),
      body: Container(
          child: StreamBuilder(
              stream: FirebaseFirestore.instance
                  .collection(widget.index)
                  .snapshots(),
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
              

                if (snapshot.data == null)
                  return Center(child: CircularProgressIndicator());
               
                else
                  return ListView.builder(
                    itemCount: snapshot.data.docs.length,
                    itemBuilder: (context, index1) {
                      DocumentSnapshot exams = snapshot.data.docs[index1];
                      return new Padding(
                        padding: new EdgeInsets.symmetric(
                          vertical: 8.0,
                          horizontal: 16.0,
                        ),
                        child: new Card(
                          elevation: 16.0,
                          shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(16.0),
                          ),
                          child: new Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              new ClipRRect(
                                child: new Image.network(
                                  exams.data()['image'],
                                  loadingBuilder:
                                      (context, child, progress) {
                                    return progress == null
                                        ? child
                                        : Container(
                                            color: Colors.black26,
                                            child: Center(
                                              child: SizedBox(
                                                  height: 250.0,
                                                  width: 200.0,
                                                  child: Center(
                                                      child: SizedBox(
                                                    width: 130.0,
                                                    height: 130.0,
                                                    child:
                                                        CircularProgressIndicator(
                                                      strokeWidth: 14.0,
                                                      backgroundColor:
                                                          Color(0xff0fd1c0),
                                                    ),
                                                  ))),
                                            ),
                                          );
                                  },
                                ),
                                borderRadius: BorderRadius.only(
                                  topLeft: new Radius.circular(16.0),
                                  topRight: new Radius.circular(16.0),
                                ),
                              ),
                              new Padding(
                                padding: new EdgeInsets.all(16.0),
                                child: new Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.start,
                                  crossAxisAlignment:
                                      CrossAxisAlignment.start,
                                  children: <Widget>[
                                    new Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.end,
                                      children: <Widget>[
                                        new FloatingActionButton(
                                          onPressed: () {},
                                          heroTag: null,
                                          child: Icon(Icons.share_outlined),
                                          backgroundColor:
                                              Colors.orangeAccent,
                                        ),
                                        SizedBox(
                                          width: 16.0,
                                          height: 10.0,
                                        ),
                                        new FloatingActionButton(
                                          onPressed: () {
                                        
                                          },
                                          heroTag: null,
                                          child: Icon(Icons.arrow_downward),
                                          backgroundColor: Colors.yellow,
                                        )
                                      ],
                                    ),
                                    new SizedBox(height: 16.0),
                                    new Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        new Text(exams
                                            .data()["name"]
                                            .toString()),
                                        new Text(
                                            exams.data()["maj"].toString()),
                                        new Text(exams
                                            .data()["name"]
                                            .toString()),
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  );
              })));
    }


        );
  }


}

Android / gradle / wrapper / gradle-wrapper.properties内容:

#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

android / build.gradle内容:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath 'com.google.gms:google-services:4.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    }
}

android / app / build.gradle内容:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.ramiahmad.cordoba_university"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-analytics:17.6.0'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.firebase:firebase-auth:19.4.0'

}}
apply plugin: 'com.google.gms.google-services'

2 个答案:

答案 0 :(得分:1)

我得到了答案,首先删除您未正确使用的其他软件包,然后进行更改...

android / app / build.gradle到:

dependencies {
    classpath 'com.android.tools.build:gradle:3.5.0'
    classpath 'com.google.gms:google-services:4.3.3'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

}

android / app / build.gradle到:

 compileSdkVersion 30

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

lintOptions {
    disable 'InvalidPackage'
}

defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.ramiahmad.cordoba_university"
    minSdkVersion 21
    targetSdkVersion 30
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    multiDexEnabled true
}

Android / gradle / wrapper / gradle-wrapper.properties到:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip

答案 1 :(得分:0)

这与您共享的代码无关。再次尝试flutter cleanflutter run