簡體   English   中英

任務 ':app:compileFlutterBuildDebug' 執行失敗。> Process 'command 'C:\\src\\flutter\\bin\\flutter.bat'' 以非零退出值 1 完成

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM