実現したいこと
メソッドを他のclassから呼び出して、
String型の値を取得したいのですが、
エラーになり呼び出すことができません。
ご教示お願いいたします。
発生している問題・エラーメッセージ
════════ Exception caught by widgets library ═══════════════════════════════════ The following _TypeError was thrown building: type 'Future<String>' is not a subtype of type 'String' in type cast
該当のソースコード
呼び出したいメソッド
dart
1 static Future<String> fetchTest(String uid) async { 2 String image = ''; 3 try { 4 final snapshot = await _userCollection.doc(uid).get(); 5 image = snapshot.data()!['image_path']; 6 } catch (e) { 7 print('失敗 ===== $e'); 8 } 9 return image; 10 }
呼び出し側
dart
1~~~~~ 2itemBuilder: (context, index) { 3 ~~~~~~~ 4 String imageP = data['sender_id']; 5 String imagePa = TestFirestore.fetchTest(imageP) as String; 6 ~~~~~~~ 7} 8~~~~~
試したこと
as Stringを付けないと、キャストしろと怒られるので、キャストして変数に入れるようにしていますが、
うまくいきません
static Future<String>をstatic Future<dynamic>にすると以下のエラーになります。
type 'Future<dynamic>' is not a subtype of type 'String' in type cast
補足情報(FW/ツールのバージョンなど)
Flutter 3.10.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 84a1e904f4 (4 weeks ago) • 2023-05-09 07:41:44 -0700
Engine • revision d44b5a94c9
Tools • Dart 3.0.0 • DevTools 2.23.1

下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/06/15 13:04