inflearn logo
강의

講義

知識共有

Flutter 初級 - Http通信、状態管理

null 관련 오류

318

lynn1602

投稿した質問数 6

0

스크린샷 2022-12-08 오후 9.57.32.png-

현재 AnimatedContainer 위젯 안에서 color를 null로 주고 decoration 안에 컬러 변수값을 넣었을 때 이러한 오류가 나타납니다. 어떻게 해결할 수 있나요?

Flutter android 웹앱 ios

回答 1

0

survivalcoding

에러의 원인이 다른 곳에 있는 것 같습니다.

전체 코드를 올려주시면 확인해 보겠습니다.

0

lynn1602

min.dart코드와 AnimatedContainer.dart 코드입니다.


import 'package:flutter/material.dart';
import 'src/MyAnimatedContainer.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {


  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false, //우측 상단 debug 제거

      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: Colors.black,
      ),

      home: MyHomePage(),

    );
  }
}

class MyHomePage extends StatefulWidget {
  // const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (context)=> MyAnimatedContainer())),
        child: Text("run animated Container"),
      ),
    );
  }
}

0

lynn1602

import 'dart:math';

import 'package:flutter/material.dart';

class MyAnimatedContainer extends StatefulWidget {


  @override
  _MyAnimatedContainerState createState() => _MyAnimatedContainerState();
}

class _MyAnimatedContainerState extends State<MyAnimatedContainer> {
  double _width = 50;
  double _height = 50;
  Color _color = Colors.teal;
  BorderRadius _borderRadius = BorderRadius.circular(8);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedContainer'),
      ),
      body: Center(
        child: AnimatedContainer(
          width: _width,
          height: _height,
          color: _color,  
          decoration: BoxDecoration(
            borderRadius: _borderRadius,
            color: _color
          ),
          duration: Duration(seconds: 1),  
          curve: Curves.fastOutSlowIn,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          setState(() {
            final random = Random();
            _width= random.nextInt(300).toDouble();
            _height= random.nextInt(300).toDouble();
            _color = Color.fromRGBO(random.nextInt(256), random.nextInt(256), random.nextInt(256), 1);
            _borderRadius = BorderRadius.circular(random.nextInt(100).toDouble());
          });
        },
        child: Icon(Icons.play_arrow),
      ),
    );
  }
}

1

survivalcoding

보내 주신 코드에는 AnimatedContainer 의 color 속성이 null 이 아닙니다.

decoration 에서 color 속성을 사용하면 AnimatedContainer의 color 속성은 제거해야 합니다.

잘 되는 것 확인했습니다.

그리고 보내주신 코드로는 위에서 말씀해 주신 에러는 재현되지 않네요.

ListenableBuilder가 안되요..material import 했는데도 자동완성이 안뜨고 빨간줄이 뜨네요 ..

0

150

2

pubspec.yaml 파일에서 Pub get이 안됩니다.

0

889

1

[네이티브연동 관련 질문]

0

334

1

네이티브코드 연동할때 네이티브 디버깅은 어떻게 하나요?

0

477

1

CartBloc 대체 코드 올립니다.

0

455

2

강사님처럼 코드가 화면 범위 밖으로 나갔을 때 자동으로 줄바꿈은 어떻게 하나요?

0

969

1

ShowSnackBar Undefined 오류

0

419

1

화면새로고침 질문.

0

1491

1

flutter 멀티이미지업로드 질문

0

686

1

fluuter php mysql 글작성

0

435

2

플러터 서버에 이미지 저장하기

0

880

1

오류 질문입니다.

0

258

1

강의를 보다 listview 기능에 의문이 생겨 질문드립니다.

0

748

1

inheritedFromWidgetOfExactType 가 없습니다.

0

231

1

The operator '<' can't be unconditionally invoked because the receiver can be 'null'.

0

1114

1

해결이 어려운 부분이 있습니다 ㅠㅠ

0

1243

2

네이티브코드 연동 AS 21.11.11 update 영상 중

0

213

1

특정 함수로의 객체 전달 방법 문의 드립니다.

0

467

1

플러터 최적의 폴더 구조를 잡고 싶습니다.

0

1029

1

mainActive.kt 오류

0

318

1

xcode 오류가 납니다.

0

360

1

dart 2.12 이상 버전에서 json object null safety 처리는 어떻게 하나요?

0

631

1

json to dart

0

177

1

no sound null safety 오류

0

199

1