skip to Main Content

I was trying to make a popup that could toggle with the floating button but now I am just trying to get the default example working. The center, column, methods and mainaxisalignment identifier(I think that is the name) suddenly stopped working. I was trying to fix it in one file but I realized that its happening in all my files even in different projects. I’m new to flutter so I don’t know if I need to completely refresh my system or if there is a specific part I need to redownload/ refresh. Any help would be greatly appreciated. Thank you.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
// ERRORS HAPPEN IN THE FOLLOWING THREE LINES
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
The method 'Center' isn't defined for the type '_MyHomePageState'.
Try correcting the name to the name of an existing method, or defining a method named 'Center'.dartundefined_method

The method 'Column' isn't defined for the type '_MyHomePageState'.
Try correcting the name to the name of an existing method, or defining a method named 'Column'.dartundefined_method

Undefined name 'MainAxisAlignment'.
Try correcting the name to one that is defined, or defining the name.dartundefined_identifier

2

Answers


  1. First, try to run flutter pub get in the terminal in the project root and see if it fixes the problem. if it doesn’t and you are using Android studio, try to make sure that Dart is enabled in the project settings, if still from the Android Studio File menu invalidate the cache and restart.

    Login or Signup to reply.
    1. First run "flutter clean" then "flutter pub get".
    2. if still does not help then check Dart is enabled or not.
    3. if still not helping then click on Invalidate Cache.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search