簡體   English   中英

Dart Flutter:為函數指定數據類型

[英]Dart Flutter: Specifying data types for a function

這是一個沒有錯誤的函數,它接受一個String和一個具有泛型返回類型TFunction

  Stream<List<T>> _collectionStream<T>({
    @required String path,
    @required T builder(Map<String, dynamic> map),
  }) {
    final jobsCollection = _instance.collection(path);
    final jobsSnapshots = jobsCollection.snapshots();

    return jobsSnapshots.map((snapshot) => snapshot.docs
        .map((document) => builder(document.data()))
        .toList());
  }

我在_collectionStream<T>部分時遇到了麻煩,據我所知,這意味着它需要一個泛型類型T的變量? 如果是這樣,它也需要一個String變量,但是當我將它添加到_collectionStream<String ,T>我得到了異常(見下文):

    final jobsCollection = _instance.collection(path); //HERE: The argument type 'String' can't be assigned to the parameter type 'String'.
    final jobsSnapshots = jobsCollection.snapshots();

    return jobsSnapshots.map((snapshot) => snapshot.docs
        .map((document) => builder(document.data())) // HERE: The argument type 'Map<String, dynamic>' can't be assigned to the parameter type 'Map<String, dynamic>'.
        .toList());

問題是<String, T>是指_collectionStream方法中的path參數,我想寫<String, T>來指的是該方法采用的兩個參數,我不是創建一個新類稱為字符串。 或者我是否錯誤地使用了method<TypeOne, TypeTwo>({varOne, varTwo})語法?

感謝您向我解釋為什么會出現此錯誤,並請幫助我理解_collectionStream<intOrWhatever>語法及其含義。

這是我嘗試運行應用程序時的錯誤日志:

Launching lib\main.dart on AOSP on IA Emulator in debug mode...
lib/services/database.dart:68:49: Error: The argument type 'String/*1*/' can't be assigned to the parameter type 'String/*2*/'.
 - 'String/*1*/' is from 'package:time_tracking_andrea_udemy_project/services/database.dart' ('lib/services/database.dart').
 - 'String/*2*/' is from 'dart:core'.
    final jobsCollection = _instance.collection(path);
                                                ^
lib/services/database.dart:72:45: Error: The argument type 'Map<String/*1*/, dynamic>' can't be assigned to the parameter type 'Map<String/*2*/, dynamic>'.
 - 'Map' is from 'dart:core'.
 - 'String/*1*/' is from 'dart:core'.
 - 'String/*2*/' is from 'package:time_tracking_andrea_udemy_project/services/database.dart' ('lib/services/database.dart').
        .map((document) => builder(document.data()))

在您的項目中,有一個重復的類String 它可能在另一個項目中,但從包路徑來看,它似乎是您創建的。 此類將與導入該文件的項目中任何位置的內置String類隱藏並發生沖突。

要解決沖突,您需要將String類重命名為不沖突的內容(如DatabaseString )或使用前綴導入該文件:

import 'package:time_tracking_andrea_udemy_project/services/database.dart' as db;

// elsewuere

final s = new db.String();

我會推薦第一個選項。 作為一般規則,不要將您的東西命名為與項目中的其他東西或尤其是核心 Dart 庫中的東西沖突的東西。 這只是乞求遇到這樣的碰撞錯誤。

函數定義中括號中的類型 <String, T> 是函數使用的泛型類型的名稱。 它們不涉及函數參數的類型。 函數參數的類型就在那里用參數名稱指定:String path 和 T builder(Map)。

正確的函數,括號中只有一個 T,意味着你的函數的每個調用點在編譯時選擇一個靜態類型 T,使函數調用具有正確的類型,給定參數類型和預期的返回值類型,並調用一個使用在函數定義中出現 T 的地方使用該靜態類型的函數的實現。

當您添加第二個類型變量 <String, T> 時,這意味着在您的函數定義中,T 和 String 是泛型類型的名稱,在您的函數使用(調用)的每個地方都將替換為特定類型。 所以這個名字“String”覆蓋並隱藏了實際的String類。 因此,當您在函數中使用類型“String”時,它並不意味着“來自 dart:core 的 String 類”,它意味着“一種未知類型,我在這里調用 String,當我編寫一個在我使用該函數的代碼中對該函數的特定調用”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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