Flutter代码报错:Container方法未定义于_LoginScreenState类型
解决Flutter中"The method 'Container' isn't defined for the type '_LoginScreenState'"错误
错误提示:
"The method 'Container' isn't defined for the type '_LoginScreenState'. Try correcting the name to the name of an existing method, or defining a method named 'Container'."
问题原因及解决方案
你的代码中Container的写法本身是正确的,出现这个错误通常是以下几个原因导致,对应解决方法如下:
确认导入Flutter Material包
Container是Flutter Material库中的组件,必须确保文件顶部导入了该包:import 'package:flutter/material.dart';缺少这个导入时,IDE会将
Container识别为_LoginScreenState类的自定义方法,从而抛出错误。清理IDE缓存并重启
有时IDE的缓存会导致错误的代码识别,执行以下操作:- Android Studio:点击
File -> Invalidate Caches...,选择"Invalidate and Restart" - VS Code:按下
Ctrl+Shift+P,输入Reload Window重载窗口
- Android Studio:点击
刷新依赖并检查SDK版本
运行以下命令确保依赖加载正常,并使用稳定版Flutter SDK:flutter pub get flutter upgrade
你的源代码(供参考)
class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State<LoginScreen> createState() => _LoginScreenState(); } class _LoginScreenState extends State<LoginScreen> { TextEditingController emailController = TextEditingController(text: ''); TextEditingController passwordController = TextEditingController(text: ''); @override Widget build(BuildContext context) { AuthProvider authProvider = Provider.of<AuthProvider>(context); return Scaffold( backgroundColor: Colors.white, body: SafeArea( child: Padding( padding: const EdgeInsets.all(16), child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.fromLTRB(145, 30, 145, 10), child: Image.asset( "assets/logo2.png", width: 75, height: 20, ), ), Padding( padding: const EdgeInsets.fromLTRB(128, 0, 128, 0), child: Text( "Masuk", style: MyStyle.titleText, ), ), SizedBox( height: 100, ), Text( "Email", style: MyStyle.labelFormText, ), SizedBox( height: 12, ), Container( padding: EdgeInsets.all(16), decoration: BoxDecoration( border: Border.all(width: 1), borderRadius: BorderRadius.circular(15), ), height: 56, // width: 343, child: Column( children: [ Expanded( child: TextFormField( controller: emailController, style: GoogleFonts.poppins( fontSize: 16, color: MyColor.primaryTextColor), decoration: InputDecoration.collapsed( hintText: "Masukkan email anda", hintStyle: GoogleFonts.poppins( fontSize: 14, fontWeight: FontWeight.normal)), ), ) ], ), ), SizedBox( height: 19, ), Text( "Password", style: MyStyle.labelFormText, ), SizedBox( height: 12, ), Container( padding: EdgeInsets.all(16), decoration: BoxDecoration( border: Border.all(width: 1), borderRadius: BorderRadius.circular(15), ), height: 56, // width: 343, child: Row( children: [ Expanded( child: TextFormField( obscureText: true, controller: passwordController, style: GoogleFonts.poppins( fontSize: 16, color: MyColor.primaryTextColor), decoration: InputDecoration.collapsed( hintText: "Masukkan password anda", hintStyle: GoogleFonts.poppins( fontSize: 14, fontWeight: FontWeight.normal)), ), ) ], ), ), SizedBox( height: 30, ), Container( height: 56, width: double.infinity, decoration: BoxDecoration( borderRadius: BorderRadius.circular(15), color: MyColor.primaryColor), child: TextButton( onPressed: () => { if (authProvider.login( email: emailController.text, password: passwordController.text)) {Navigator.pushNamed(context, '/home')} else { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: const Text( "Username atau password anda salah!"))) } }, child: Text( "Masuk", style: MyStyle.labelFormText.copyWith( color: Colors.white, fontWeight: FontWeight.bold), ))), SizedBox( height: 15, ), Container( width: double.infinity, height: 56, decoration: BoxDecoration( borderRadius: BorderRadius.circular(15), color: MyColor.secondaryColor), child: TextButton( onPressed: () => {Navigator.pushNamed(context, '/signup')}, child: Text( "Daftar", style: MyStyle.labelFormText.copyWith( color: MyColor.primaryColor, fontWeight: FontWeight.bold), ))), SizedBox( height: 15, ), Container( padding: EdgeInsets.symmetric(horizontal: 50), width: double.infinity, height: 56, decoration: BoxDecoration( border: Border.all(width: 1, color: Colors.black), borderRadius: BorderRadius.circular(15), ), child: Row( children: [ Image.asset( "assets/logo_google.png", width: 30, height: 30, ), SizedBox(width: 5), Expanded( child: TextButton( onPressed: () {}, child: Text( "Masuk dengan Google", style: MyStyle.labelFormText.copyWith( color: Colors.black, fontWeight: FontWeight.normal), ), )) ], )) ], ), ), ), ), ); } }
内容的提问来源于stack exchange,提问作者Bintang
相关产品推荐
相关产品推荐

