![](/img/trans.png)
[英]Execution failed for task ':app:dexDebug'.Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 2
[英]Execution failed for task ':app:compileFlutterBuildDebug'.> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1
在此页面中显示语法错误
QueryDocumentSnapshot 文档参数类型 'Map<String, dynamic> Function()' 不能分配给参数类型 'Map<String, dynamic>'.dartargument_type_not_assignable
lib/services/firestore_service.dart
import 'package:firestoreD/cloud_firestore.dart';
import 'package:firestoreD/models/product.dart';
class FirestoreService {
// ignore: deprecated_member_use
Firestore _db = Firestore.instance;
Future<void> saveProduct(Product product){
// ignore: deprecated_member_use
return _db.collection('products').document(product.productId).setData(product.toMap());
}
// ignore: missing_return
Stream<List<Product>> getProducts(){
// ignore: deprecated_member_use
return _db.collection('products').snapshots().map((snapshot) => snapshot.documents.map((document)
=>Product.fromFirestore(document.data)).toList());
}
Future<void> removeProduct(String productId){
// ignore: deprecated_member_use
return _db.collection('products').document(productId).delete();
}
}
在此页面中显示语法错误
产品 Product.fromFirestore(Map<String, dynamic> firestore) 包:firestore_crud/models/product.dart
'fromFirestore' 不能用于命名此类中的构造函数和静态字段。 尝试重命名构造函数或 field.dart(conflicting_constructor_and_static_field)
lib/models/product.dart
class Product{
final String productId;
final String name;
final double price;
static var fromFirestore;
Product({this.productId,this.price, this.name});
Map<String,dynamic> toMap(){
return {
'productId' : productId,
'name' : name,
'price' : price
};
}
Product.fromFirestore(Map<String,dynamic> firestore)
: productId = firestore['productId'],
name = firestore['name'],
price = firestore['price'];
}
lib/main.dart
import 'package:firestoreD/providers/product_provider.dart';
import 'package:firestoreD/screens/products.dart';
import 'package:firestoreD/services/firestore_service.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
final firestoreService = FirestoreService();
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => ProductProvider()),
StreamProvider(create: (context)=> firestoreService.getProducts()),
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Products(),
),
);
}
}
pubspec.yaml
name: firestoreD
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as
CFBundleVersion.
# Read more about iOS versioning at
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
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.3
firebase_core: ^0.5.0
cloud_firestore: ^0.14.0+2
provider: ^4.3.2+1
uuid: ^2.2.0
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/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.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/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.dev/custom-fonts/#from-packages
运行时显示此类错误
lib/models/product.dart:18:3: 错误:与成员“fromFirestore”发生冲突。 Product.fromFirestore(Map<String,dynamic> firestore) ^ lib/models/product.dart:6:14: 错误:与构造函数“Product.fromFirestore”冲突。 来自Firestore的静态变量; ^
FAILURE:构建失败,出现异常。
其中:脚本 'C:\\src\\flutter\\packages\\flutter_tools\\gradle\\flutter.gradle' 行:896
出了什么问题:任务 ':app:compileFlutterBuildDebug' 执行失败。
进程 'command 'C:\\src\\flutter\\bin\\flutter.bat'' 以非零退出值 1 结束
尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。 使用 --info 或 --debug 选项运行以获得更多日志输出。 使用 --scan 运行以获得完整的见解。
在https://help.gradle.org获取更多帮助
在您的类模型上使用工厂。
删除这一行 --> " static var fromFirestore; "
更新你的班级是这样的:
class Product{
final String productId;
final String name;
final double price;
Product({this.productId,this.price, this.name});
Map<String,dynamic> toMap(){
return {
'productId' : productId,
'name' : name,
'price' : price
};
}
//FACTORY parse from snapshot
factory Product.fromSnapshot(DocumentSnapshot snapshot) {
//Todo set values from snapshot
}
//FACTORY parse from Map
factory Product.fromFirestore(Map<String,dynamic> firestore){
//Todo set values from map
return {
'productId' : firestore['productId'],
'name' : firestore['name'],
'price' : firestore['price'],
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.