MENU

MENU

カテゴリー

\気になるカテゴリーをクリック!/

やきいも
文系SE
【プロフ】
 ▶ 30代半ば
 ▶ ITエンジニア13年目 | 業務システム
 ▶ 妻と息子の3人で田舎の暮らし

【サイト運営】
このブログは私の体験をもとに以下に関する情報をお届けしています。
 ▶ AI
 ▶ Java
 ▶ Flutterなど
Udemyセール!最大95%オフ!1,200円~4月の最新セール情報をみる

【Flutterエラー対応】Error: To set up CocoaPods for ARM macOS, runError: type ‘Null’ is not a subtype of type ‘String’ in type cast

この記事には広告を含む場合があります。

Flutter のアプリ開発でtype ‘Null’ is not a subtype of type ‘String’ in type castと表示されて、どう対応したら良いのか分からない。

この記事で分かること!
  • type ‘Null’ is not a subtype of type ‘String’ in type castエラーの対処法
  • nullsafetyとは

フリーランスのFlutterエンジニアとして働きたい方にオススメのエージェントはこちらで詳しく解説しています。

目次

事象「Flutterエラー対応)Error: type ‘Null’ is not a subtype of type ‘String’ in type cast」

開発環境

モデルMacBook Air 2020
CPU・GPUM1(GPU8コア GPU8コア)
メモリ16GB
SSD512GB
Flutter3.3.0
Dart2.18.0

エラー内容

======== Exception caught by widgets library =======================================================
The following _CastError was thrown building XXXX(dirty):
type 'Null' is not a subtype of type 'String' in type cast

原因:Null Safety

エラー原因のソースコード

import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';

class Quiz extends StatelessWidget {
  final List<Map<String, Object>> questions;
  final int questionIndex;
  final Function answerQuestion;

  Quiz({
    required this.questions,
    required this.answerQuestion,
    required this.questionIndex
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Question(
          questions[questionIndex]['questionText']as String
        ),
        ...(questions[questionIndex]['answers'] as List<Map<String,Object>>).map((answer) {
          return Answer(() => answerQuestion(answer['score']), answer['text']as String);
        }).toList()
      ],
    );
  }
}

dart 2.12 からNull Safetyになりました。

Null Safetyとは

Dart 2.12 より前は、アプリ実行時にNullPointerExceptionとなりシステムエラーとなってました。現在はコンパイル時にエラーとなるのです。

対処方法:to String

対応後のソースコード

import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';

class Quiz extends StatelessWidget {
  final List<Map<String, Object>> questions;
  final int questionIndex;
  final Function answerQuestion;

  Quiz({
    required this.questions,
    required this.answerQuestion,
    required this.questionIndex
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Question(
          questions[questionIndex]['questionText'].toString(),
        ),
        ...(questions[questionIndex]['answers'] as List<Map<String,Object>>).map((answer) {
          return Answer(() => answerQuestion(answer['score']), answer['text'].toString());
        }).toList()
      ],
    );
  }
}

as stringの箇所を.tostring()に修正し正常に動作。

まとめ:type ‘Null’ is not a subtype of type ‘String’ in type castはto Stringで対応

今回の記事では、Flutter のアプリ開発でtype ‘Null’ is not a subtype of type ‘String’ in type castと表示されて、どう対応したら良いのか分からない時の対処法について解説しました。

対処方法は?
  • to Stringで対応


Flutterエンジニアになるには?

初心者が中級者レベルのFlutterエンジニアなるまでの進め方をまとめました。

Flutterの学習方法を知る

Flutter をスクールで学ぶ

Flutterの副業を探す

おまけFlutter入門の完全ガイド

Flutter/Dartの基礎一覧

Flutter/Dartの入門知識として押さえておきたい内容をまとめました。学習のご参考にどうぞ。

Widget(ウィジェット) 一覧

Dart 基本文法

ライブラリ 使い方

コメント

コメントする

CAPTCHA


目次