😸

【Flutter】遭遇したエラー&&解決策まとめ③

2021/01/02に公開

前回

【Bad state: Stream has already been listened to】

◆ エラー内容

Bad state: Stream has already been listened to.

◆ 解決策

dependencies:
  rxdart: ^0.25.0
// StreamController → BehaviorSubject
final _sample = StreamController<String>();  //before
final _sample = BehaviorSubject<String>();  //after

【setState() called after dispose();エラー】

◆ エラー内容

This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree

◆ 解決策

int _count = 0;

void _sampleCounter() {
  if(mounted) {  //追加
    setState(() {
      _count++;
  });
}

【cloud_firestore/permission-denied】

◆ エラー内容

cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.

◆ 解決策

Firebaseコンソール > プロジェクト選択 > Cloud Firestore >  ルール

【Dart条件分岐】

◆ switch文

const sample1 = "犬";
const sample2 = "猫";
var pets = "ペット";

switch (pets) {
  case sample1:
    print('犬好き');
    break;

  case sample2:
    continue sample1;

  default:
    print('犬も猫も好き');
    break;
}

◆ if文

var pets = "ペット";

if (pets == "犬") {
  print('犬好き');
} else if (pets == "猫") {
  print('猫好き');
} else
  print('犬も猫も好き');
}

【setStateメソッド】

◆ setState

int _count = 0;

void _sampleCounter() {
  setState(() {
    _count++;
  });
}

参考文献

Discussion