Undefined class 'Widget'. Try changing the name to the name of an existing class, or creating a class with the name 'Widget'

huangapple go评论60阅读模式
英文:

Undefined class 'Widget'. Try changing the name to the name of an existing class, or creating a class with the name 'Widget'

问题

我已安装并开始了我的Flutter之旅,但在我的第一个项目后,所有的类和小部件都显示错误消息。起初它是正常工作的,但后来开始出现错误消息。

import 'package:flutter/material.dart';

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

class FirstApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Code The Best"),
        ),
        body: Center(
          child: Column(
            children: [
              TextButton(
                onPressed: () {},
                child: Text("Login"),
              ),
              TextButton(
                onPressed: () {},
                child: Text("Register"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

我得到了一系列错误,包括以下几个:

  1. 未定义类 'BuildContext'。
    请尝试将名称更改为现有类的名称,或创建一个名称为 'BuildContext' 的类。

  2. 未定义类 'Widget'。
    请尝试将名称更改为现有类的名称,或创建一个名称为 'Widget' 的类。

英文:

I have installed and started my flutter journey but after my first project, all the classes and widgets are showing an error message. It was working at first but I later started getting the error message.

import 'package:flutter/material.dart';

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

class FirstApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Code The Best"),
        ),
        body: Center(
          child: Column(
            children: [
              TextButton(
                onPressed: () {},
                child: Text("Login"),
              ),
              TextButton(
                onPressed: () {},
                child: Text("Register"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

And I get a number of errors including these:

  1. Undefined class 'BuildContext'.
    Try changing the name to the name of an existing class, or creating a class with the name 'BuildContext'.

2.Undefined class 'Widget'.
Try changing the name to the name of an existing class, or creating a class with the name 'Widget'.

答案1

得分: 1

尝试移除 build 文件夹并重新构建。
你可以通过以下步骤完成:

  • flutter clean
  • flutter pub get
英文:

try removing build folder and re-building it again.
You can do this by,

  • flutter clean
  • flutter pub get

huangapple
  • 本文由 发表于 2023年2月19日 08:06:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定