android - 获取可与 Cloud Firestore 配合使用的简单应用

标签 android dart google-cloud-firestore flutter

我正在关注来自网站 https://pub.dartlang.org/packages/cloud_firestore#-readme-tab- 的示例.我遵循了 README.md 中针对 Android 应用程序的所有说明。在 Firebase 控制台中创建一个 Android 应用程序,复制 google-services.json 并更新两个 buidl.gradle 文件,如下所示:

android/build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        // Add this line
        // **********************************
        classpath 'com.google.gms:google-services:3.2.0'
        // **********************************
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

android/app/build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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

android {
    compileSdkVersion 27

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.fluttercloudfirestore"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    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 {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    // Add this line
    // **********************************
    compile 'com.google.firebase:firebase-core:12.0.0'
    // **********************************
}

// Add to the bottom of the file
// **********************************
apply plugin: 'com.google.gms.google-services'
// **********************************

还更新了我的 pubspec.yaml

name: flutter_cloud_firestore
description: A new Flutter project.

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  cloud_firestore: "^0.4.0"


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies, 
  # see https://flutter.io/custom-fonts/#from-packages

当我运行 flutter packages get 时,出现以下错误:

在 flutter_cloud_firestore 中运行“flutter packages get”... 包 cloud_firestore 没有匹配 >=0.4.0 <0.5.0 的版本来自: - flutter_cloud_firestore 取决于版本 ^0.4.0 酒吧失败(一)

根据 this页面,0.4.0 是当前版本。给了什么?

最佳答案

从包主页或 pub 包管理器本身的错误消息中没有简单的方法来判断这一点,但它不适用于 Flutter 的 beta channel (这是默认的截至本文),因此您必须切换到 dev channel 。

您可以使用 flutter channel 命令更改您所在的发布 channel 。在这种情况下,如果您执意要使用 0.4.0,则需要在 dev channel 上:

$ flutter channel dev
$ flutter doctor // May not be strictly necessary, but a good check

然后 $ flutter packages get 应该正确获取包。

关于android - 获取可与 Cloud Firestore 配合使用的简单应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49680136/

相关文章:

flutter - 如何在 Dart/Flutter 的构造函数中初始化 Map

ios - 在 Swift 中存储异步 Cloud Firestore 查询结果

android - 支持的音效

android - 对话框打开动画在 Android 4.2 上不起作用

android - 如何以编程方式创建 admob 横幅?

angularjs - 如何在 Firebase Cloud Firestore 中高效执行多次读取?

javascript - Firebase函数: Node. js:迭代存储在firebase文档字段中的映射

java.util.MissingFormatArgumentException : Format specifier: s

firebase - 参数不匹配的闭包调用:flutter中的 '[]'函数

flutter - Flutter:键盘覆盖了TextFields