0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

Organization

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

前回

【Bad state: Stream has already been listened to】

◆ エラー内容

Bad state: Stream has already been listened to.

◆ 解決策

pubspec.yaml
dependencies:
  rxdart: ^0.25.0
main.dart
// 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

◆ 解決策

main.dart
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文

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

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

  case sample2:
    continue sample1;

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

◆ if文

main.dart
var pets = "ペット";

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

【setStateメソッド】

◆ setState

main.dart
int _count = 0;

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

参考文献

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?