@sysdjp

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

【Flutter】掲示版アプリのビルドエラーについて

解決したいこと

Flutterで掲示版アプリを作製しています。
Firebaseから取得したデータをListIileに渡すところでビルドエラーが発生しました。
解決方法を教えて下さい。

発生している問題・エラー

The method '[]' can't be unconditionally invoked because the receiver can be 'null'.

スクリーンショット 2021-08-14 14.53.40.png

該当するソースコード

title: Text(document.data()['content']),

スクリーンショット 2021-08-14 14.55.22.png

自分で試したこと

Nullチェックを追加したところ、別のメッセージが表示されました。

Nullチェック追加前
title: Text(document.data()['content']),

Nullチェック追加後
title: Text(document.data()!['content']),

追加後に表示されたメッセージ
The operator '[]' isn't defined for the type 'Object'.

スクリーンショット 2021-08-14 14.58.20.png

0 likes

5Answer

Flutterのバージョンを書き忘れました。

$flutter doctor -v
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.1 20G80 darwin-x64, locale
ja-JP)
• Flutter version 2.2.3 at /Users/yahatamasakazu/development/flutter
• Framework revision f4abaa0735 (6 weeks ago), 2021-07-01 12:46:11 -0700
• Engine revision 241c87ad80
• Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/yahatamasakazu/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.3, Build version 12C33
• CocoaPods version 1.10.2

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] Connected device (2 available)
• iPhone 12 Pro Max (mobile) • F6011D58-61C1-42F6-8B46-E7807A60E5BA • ios
• com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
• Chrome (web) • chrome •
web-javascript • Google Chrome 92.0.4515.131

• No issues found!

0Like

!はNullチェックをしているのではなく、強制的にNon-nullにキャストしているだけだと思います。

おそらくdocument.data()はnullなので、??かif文かはおまかせしますがNullチェックしていただければ解決できると思います。

0Like

This answer has been deleted for violation of our Terms of Service.

Your answer might help someone💌