0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

The getter 'displayName' was called on null. Receiver: null Tried calling: displayNameの解決法

Posted at

はじめに

この問題が発生したのは、下記のmy_page_screen.dartの実装を進めている時

このエラーの原因は何?

Providerでまだ値の取得が終わっていないのに、Widgetの描画が始まっていることが原因だと思われる:rolling_eyes:

「null」の場合の条件を追加しよう

 child: Column(
            children: <Widget>[
              Consumer<UserProvider>(builder: (context, model, child) {
                final userData = model.userModels;
                return userData == null
                    ? Container()
                    : Row(
                        children: <Widget>[
                          Container(

このように?:を使うことでウィジェットの表示を条件によって切り替えられます!

この場合、
userData == nullなら空っぽのContainer()を表示
userData == nullではないなら、userDataを使って値を表示するRow()を表示

return userData == null ? Container() : Row()

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?