Pls can anyone help, Flutter is showing this message Expected to find ']'. in main.dart file in VS code, Pls check the bottom left of the editor pls

Viewed 33
@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.all(28),
        color: Colors.blue.shade800,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(Icons.people, size: 78),
            const TextField(
              decoration: InputDecoration(label: Text("Login:")),
            ),
            
             onChanged: _controller.setLogin,

            const TextField(
              decoration: InputDecoration(label: Text("Senha:")),
              obscureText: true,
              onChanged: _controller.setPass,
            ),
            const SizedBox(
              height: 25,
            ),
            ElevatedButton(
              onPressed: () {
                _controller.auth();
              },
              child: const Text("Login"),
            ),
          ],
        ),
      ),
    );
  }
}
3 Answers

Don't use const when you're passing values and methods from controller. It'll give you an error.

Also your onChanged method is outside the TextField. Here's the workig code for you:

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.all(28),
        color: Colors.blue.shade800,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(Icons.people, size: 78),
             TextField(
              decoration:const InputDecoration(label: Text("Login:")),
              onChanged: _controller.setLogin,
            ),
             TextField(
              decoration: InputDecoration(label: Text("Senha:")),
              obscureText: true,
              onChanged: _controller.setPass,
            ),
            const SizedBox(
              height: 25,
            ),
            ElevatedButton(
              onPressed: () {
                _controller.auth();
              },
              child: const Text("Login"),
            ),
          ],
        ),
      ),
    );
  }

First move your onChanged into the TextField, then in order to use onChanged you should remove const.Also you have an extra }. Try this:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.all(28),
        color: Colors.blue.shade800,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(Icons.people, size: 78),
            TextField(
              decoration: InputDecoration(label: Text("Login:")),
              onChanged: _controller.setLogin,
            ),
            TextField(
              decoration: InputDecoration(label: Text("Senha:")),
              obscureText: true,
              onChanged: _controller.setPass,
            ),
            const SizedBox(
              height: 25,
            ),
            ElevatedButton(
              onPressed: () {
                _controller.auth();
              },
              child: const Text("Login"),
            ),
          ],
        ),
      ),
    );
  }

onChanged should go inside TextField and you will also need to remove const from your TextField. Here's the full working code:

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Container(
      padding: const EdgeInsets.all(28),
      color: Colors.blue.shade800,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Icon(Icons.people, size: 78),
          TextField(
            decoration: InputDecoration(label: Text("Login:")),
            onChanged: _controller.setLogin,
          ),
          TextField(
            decoration: InputDecoration(label: Text("Senha:")),
            obscureText: true,
            onChanged: _controller.setPass,
          ),
          const SizedBox(
            height: 25,
          ),
          ElevatedButton(
            onPressed: () {
              _controller.auth();
            },
            child: const Text("Login"),
          ),
        ],
      ),
    ),
  );
}
Related