首页 > 解决方案 > 我得到了小部件库捕获的异常

问题描述

import 'package:app1/answer.dart';
import 'package:flutter/material.dart';
import 'question.dart';
import 'answer.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
 @override
 MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
 var questionindex = 0;
 void answerquestion() {
   if (questionindex == 1) questionindex = -1;
   setState(() {
     questionindex++;
   });
   print(questionindex);
   print("answer chosen");
 }

 final List<Map<String, Object>> question = [
   {
     'questionText': 'what is your fav color?',
     'answers': ['black', 'green', 'blue', 'yellow']
   },
   {
     'questionText': 'whats your fav animal?',
     'answers': ['rabbit', 'dog', 'cat', 'monkey']
   },
 ];

 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     theme: ThemeData(
         elevatedButtonTheme: ElevatedButtonThemeData(
             style: ButtonStyle(
       backgroundColor: MaterialStateProperty.all(Colors.red),
     ))),
     home: Scaffold(
       appBar: AppBar(
         title: Text("quiz app"),
       ),
       body: Container(
         child: Column(
           children: <Widget>[
             Question(question[questionindex]['questionText'] as String),
             ...(question[questionindex]['answers'] as List<String>).map(
               (answer1) {
                 return Answer(answerquestion, answer1);
               },
             ).toList(),
           ],
         ),
       ),
     ),
   );
 }
}

这些是我得到的例外

═════ Exception caught by rendering library ═════════════════════════════════
A RenderFlex overflowed by 399452 pixels on the bottom.
The relevant error-causing widget was
Column

Exception caught by widgets library ═══════════════════════════════════
setState() or markNeedsBuild() called during build.
The relevant error-causing widget was
Answer

标签: flutterdart

解决方案


添加您的列小部件SingleChildScrollView扩展容器

SingleChildScrollView(
child:Column(
           children: <Widget>[
             Question(question[questionindex]['questionText'] as String),
             ...(question[questionindex]['answers'] as List<String>).map(
               (answer1) {
                 return Answer(answerquestion, answer1);
               },
             ).toList(),
           ],
         ),
),

推荐阅读