0
votes

This is my code and the error I'm getting in Visual Studio Code. It used to work before, not sure if an update messed it up. I have other Flutter programs running just fine, not sure what the problem is with this one.

// 1) Create a new Flutter App (in this project) and output an AppBar and some text
// below it
// 2) Add a button which changes the text (to any other text of your choice)
// 3) Split the app into three widgets: App, TextControl & Text

import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  Random _stringIndex = new Random();
  var _randomStringIndex;

  final _strings = const [
    "Hello",
    "This is a string",
    "This is random",
  ];

  void _changeText() {
    setState(() {
      _randomStringIndex = _stringIndex.nextInt(_strings.length);
    });

    print(_strings[_randomStringIndex]);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App'),
        ),
        body: Center(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  onPressed: _changeText,
                ),
                Text(
                  _strings[_randomStringIndex],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Launching lib/main.dart on Android SDK built for x86 in debug mode... ✓ Built build/app/outputs/apk/debug/app-debug.apk. Connecting to VM Service at ws://127.0.0.1:40605/I0TqLbk5jw4=/ws D/EGL_emulation( 7151): eglMakeCurrent: 0xe1b1a3c0: ver 2 0 (tinfo 0xe1b0f770) D/eglCodecCommon( 7151): setVertexArrayObject: set vao to 0 (0) 1 0 I/flutter ( 7151): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 7151): The following ArgumentError was thrown building MyApp(dirty, state: _MyAppState#202c3): I/flutter ( 7151): Invalid argument(s) I/flutter ( 7151): I/flutter ( 7151): The relevant error-causing widget was: I/flutter ( 7151): MyApp I/flutter ( 7151):
package:flutter_assignment/main.dart:11 I/flutter ( 7151): I/flutter ( 7151): When the exception was thrown, this was the stack: I/flutter ( 7151): #0 List.[] (dart:core-patch/array.dart:169:52) I/flutter ( 7151): #1 _MyAppState.build package:flutter_assignment/main.dart:57 I/flutter ( 7151): #2
StatefulElement.build package:flutter/…/widgets/framework.dart:4619 I/flutter ( 7151): #3 ComponentElement.performRebuild package:flutter/…/widgets/framework.dart:4502 I/flutter ( 7151): #4
StatefulElement.performRebuild package:flutter/…/widgets/framework.dart:4675 I/flutter ( 7151): #5
Element.rebuild package:flutter/…/widgets/framework.dart:4218 I/flutter ( 7151): #6 ComponentElement._firstBuild package:flutter/…/widgets/framework.dart:4481 I/flutter ( 7151): #7
StatefulElement._firstBuild package:flutter/…/widgets/framework.dart:4666 I/flutter ( 7151): #8
ComponentElement.mount package:flutter/…/widgets/framework.dart:4476 I/flutter ( 7151): #9 Element.inflateWidget package:flutter/…/widgets/framework.dart:3446 I/flutter ( 7151): #10
Element.updateChild package:flutter/…/widgets/framework.dart:3214 I/flutter ( 7151): #11 RenderObjectToWidgetElement._rebuild package:flutter/…/widgets/binding.dart:1148 I/flutter ( 7151): #12
RenderObjectToWidgetElement.mount package:flutter/…/widgets/binding.dart:1119 I/flutter ( 7151): #13
RenderObjectToWidgetAdapter.attachToRenderTree. package:flutter/…/widgets/binding.dart:1061 I/flutter ( 7151): #14
BuildOwner.buildScope package:flutter/…/widgets/framework.dart:2607 I/flutter ( 7151): #15
RenderObjectToWidgetAdapter.attachToRenderTree package:flutter/…/widgets/binding.dart:1060 I/flutter ( 7151): #16
WidgetsBinding.attachRootWidget package:flutter/…/widgets/binding.dart:941

2

2 Answers

0
votes

You can copy paste run full code below
You need to init _randomStringIndex
code snippet

var _randomStringIndex = 0;

working demo

enter image description here

full code

import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  Random _stringIndex = new Random();
  var _randomStringIndex = 0;

  final _strings = const [
    "Hello",
    "This is a string",
    "This is random",
  ];

  void _changeText() {
    setState(() {
      _randomStringIndex = _stringIndex.nextInt(_strings.length);
    });

    print(_strings[_randomStringIndex]);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App'),
        ),
        body: Center(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  onPressed: _changeText,
                ),
                Text(
                  _strings[_randomStringIndex],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
0
votes

Try to run this command inside your project (terminal):

flutter clean

and then run it again!