Skip to main content

Flutter Apprentice9章と10章で分からなかったことをまとめる

Flutter Apprentice7章と8章で分からなかったことをまとめる
の続編

Chapter9 Shared Preferences
Chapter10 Serialization With JSON
を読んだので分からなかったことをまとめる


Dart
・floor
小数点の切り捨て
https://dev-yakuza.posstree.com/flutter/dart/ceil-floor-round/

・factory
factoryメソッドはfactoryの修飾子をつける
https://qiita.com/takyam/items/f6a895b873241ce937fb

 

Flutter
・flutter_slidable
スライドでメニュー出すやつができる。
https://pub.dev/packages/flutter_slidable

・shared_preference plugin
iOSならUserDefaultになる。
もちろんsecureな情報はここに保存してはダメ。
flutter_secure_storageを使えとのこと。

・FocusScope.of(context)
unfocusでフォーカスを外したりできる
https://zenn.dev/fukutan/articles/49e32069f266b7

・PopupMenuButton
overlay menuが出てくる
https://flutter.keicode.com/basics/popupmenubutton.php

・build_runner
flutter pub run build_runner build
flutter pub run build_runner watch
>watch: Runs a persistent build server that watches the files system for edits and does rebuilds as necessary.
watchなら監視してくれる。
https://pub.dev/packages/build_runner

 

エラー系
・build_runnerでのエラー
以下のエラーが出たら

lib/network/recipe_model.g.dart:16:25: Error: The argument type 'List<APIHits?>?' can't be assigned to the parameter type 'List<APIHits>' because 'APIHits?' is nullable and 'APIHits' isn't.
 - 'List' is from 'dart:core'.
 - 'APIHits' is from 'package:recipes/network/recipe_model.dart' ('lib/network/recipe_model.dart').
    hits: (json['hits'] as List)
                        ^
lib/network/recipe_model.g.dart:36:9: Error: The argument type 'APIRecipe?' can't be assigned to the parameter type 'APIRecipe' because 'APIRecipe?' is nullable and 'APIRecipe' isn't.
 - 'APIRecipe' is from 'package:recipes/network/recipe_model.dart' ('lib/network/recipe_model.dart').
        ? null
        ^
lib/network/recipe_model.g.dart:50:39: Warning: Operand of null-aware operation '?.' has type 'List<dynamic>' which excludes null.
 - 'List' is from 'dart:core'.
    ingredients: (json['ingredients'] as List)

以下で解決
json_serializable: ^4.1.4
json_annotation: ^4.1.0

 

関連記事:

Pocket