実現したいこと
- FlutterでFirebase storageへの画像のアップロードの方法を知り、今後の開発の知見を得たい。
前提
FlutterでのFirebaseへの画像のアップロードの方法を動画を見ながら学習していますが、受け取るべき変数というものをどういったものを設定するのが良いかというのがいまいち掴めていないために、開発が止まっており、1時間近く経っても状況が改善しないため、有識者の方にご教示していただきたいなと考えています。
発生している問題・エラーメッセージ
Undefined name 'uid'. Try correcting the name to one that is defined, or defining the name.
該当のソースコード
post_page.dart
1import 'dart:io'; 2 3import 'package:chat_app/firestore/post_firestore.dart'; 4import 'package:chat_app/model/post.dart'; 5import 'package:chat_app/utils/authentication.dart'; 6import 'package:chat_app/utils/function_utils.dart'; 7import 'package:cloud_firestore/cloud_firestore.dart'; 8import 'package:flutter/material.dart'; 9 10class PostPage extends StatefulWidget { 11 const PostPage({super.key}); 12 13 @override 14 State<PostPage> createState() => _PostPageState(); 15} 16 17class _PostPageState extends State<PostPage> { 18 TextEditingController contentController = TextEditingController(); 19 File? image; 20 21 @override 22 Widget build(BuildContext context) { 23 return Scaffold( 24 backgroundColor: Colors.transparent, 25 appBar: AppBar( 26 shape: const RoundedRectangleBorder( 27 borderRadius: BorderRadius.only( 28 topLeft: Radius.circular(30), 29 topRight: Radius.circular(30), 30 ), 31 ), 32 elevation: 0, 33 iconTheme: const IconThemeData( 34 color: Colors.black, 35 ), 36 centerTitle: false, 37 title: const Text( 38 "Create post", 39 style: TextStyle( 40 color: Colors.black, 41 fontWeight: FontWeight.bold, 42 ), 43 ), 44 actions: [ 45 OutlinedButton( 46 onPressed: () async { 47 if (contentController.text.isNotEmpty && image != null) { 48 Post newPost = Post( 49 content: contentController.text, 50 postAccountId: Authentication.myAccount!.id, 51 id: '', 52 imagePath: '', 53 postTime: Timestamp.now(), 54 ); 55 await FunctionUtils.uploadImage(uid, image); 56 var result = await PostFirestore.addPost(newPost); 57 if (result == true) { 58 if (!mounted) return; 59 Navigator.pop(context); 60 } 61 } 62 }, 63 child: const Text( 64 "Post", 65 style: TextStyle( 66 color: Colors.black, 67 fontWeight: FontWeight.bold, 68 fontSize: 20, 69 ), 70 ), 71 ), 72 ], 73 ), 74 body: Padding( 75 padding: const EdgeInsets.all(16.0), 76 child: Column( 77 children: [ 78 Expanded( 79 child: TextField( 80 controller: contentController, 81 maxLines: null, 82 decoration: const InputDecoration( 83 hintText: "What's on your mind?", 84 border: InputBorder.none, 85 ), 86 cursorColor: Colors.black, 87 ), 88 ), 89 const SizedBox( 90 height: 30, 91 ), 92 image == null 93 ? const SizedBox() 94 : ClipRRect( 95 borderRadius: BorderRadius.circular(24.0), 96 child: SizedBox( 97 height: MediaQuery.of(context).size.height * 0.3, 98 width: MediaQuery.of(context).size.width * 0.9, 99 child: Image.file( 100 image!, 101 fit: BoxFit.cover, 102 ), 103 ), 104 ), 105 const Divider(height: 1), 106 ListTile( 107 leading: const Icon(Icons.photo_library), 108 title: const Text("Add a photo"), 109 onTap: () async { 110 var result = await FunctionUtils.getImageFromGallery(); 111 setState(() { 112 image = File(result.path); 113 }); 114 }, 115 ), 116 ], 117 ), 118 ), 119 ); 120 } 121} 122 123
function_utils.dart
1import 'dart:io'; 2 3import 'package:firebase_storage/firebase_storage.dart'; 4import 'package:flutter/foundation.dart'; 5import 'package:image_picker/image_picker.dart'; 6 7class FunctionUtils { 8 static Future<dynamic> getImageFromGallery() async { 9 ImagePicker picker = ImagePicker(); 10 final pickedFile = await picker.pickImage(source: ImageSource.gallery); 11 return pickedFile; 12 // if (pickedFile != null) { 13 // setState(() { 14 // image = File(pickedFile.path); 15 // }); 16 // } 17 } 18 19 static Future<String> uploadImage(String uid, File image) async { 20 final FirebaseStorage storageInstance = FirebaseStorage.instance; 21 final Reference ref = storageInstance.ref(); 22 await ref.child(uid).putFile(image); 23 String downloadUrl = await storageInstance.ref(uid).getDownloadURL(); 24 if (kDebugMode) { 25 print("image_path: $downloadUrl"); 26 } 27 return downloadUrl; 28 } 29}
試したこと
https://www.udemy.com/course/flutter-firebase-sns/learn/lecture/28160806#content
https://youtu.be/VZZx7YONWhc
上記2つの動画を見て、Firebase Storageへの画像アップロードの方法を学習。
Firebaseへの画像アップロードへの処理を定義した後、post_page.dart55行目にて処理を呼び出し。
Undefined name 'uid'.
Try correcting the name to one that is defined, or defining the name.が発生。
エラー文をコピペしてググり、以下のページにヒット。
https://stackoverflow.com/questions/58146451/undefined-name-context-try-correcting-the-name-to-one-that-is-defined-or-de
補足情報(FW/ツールのバージョンなど)
[✓] Flutter (Channel stable, 3.7.8, on macOS 13.2.1 22D68 darwin-x64,
locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version
32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.76.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。