Radiobutton in flutter with error

Asked

Viewed 185 times

0

I’m having problems with this code, when I run the following error appears:

"Unimplemented Handling of Missing Static target".

What can it be? How can I solve this problem?

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: null,
          body: Container(
              child: Center(
            child: Radiobutton(),
          ))),
    );
  }
}

class Radiobutton extends StatefulWidget {
  @override
  RadioButtonWidget createState() => RadioButtonWidget();
}

class RadioButtonWidget extends State {
  String radioItem = '';

  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        RadioListTile(
          groupValue: radioItem,
          title: Text('Radio Button Item 1'),
          value: 'Item 1',
          onChanged: (val) {
            setState(() {
              radioItem = val;
            });
          },
        ),
        RadioListTile(
          groupValue: radioItem,
          title: Text('Radio Button Item 2'),
          value: 'Item 2',
          onChanged: (val) {
            setState(() {
              radioItem = val;
            });
          },
        ),
      ],
    );
  }
}
  • Have you tried closing the app altogether and running it again? I’m not sure, but the error seems to be something related to this.

1 answer

3


You created your Widget the wrong way:

class Radiobutton extends StatefulWidget {
  @override
  RadioButtonWidget createState() => RadioButtonWidget();
}

class RadioButtonWidget extends State<Radiobutton>{
  /// ...
}

Behold here a reading on Stateless and Stateful widgets that can help you, and also a little bit about the class State.

By the way, since the class containing the current state "State" will never be instantiated directly by you, it is good practice to use a _ in its name, example for your case:

class RadioButton extends StatefulWidget

The implementation would be:

class _RadioButton extends State<RadioButton>
  • It worked! Thank you very much! I didn’t even know I needed to make this diamond in front of the State.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.