flutter - Flutter TextField 和被调用的构造函数不是 const 构造函数
问题描述
我是 Flutter 的新手,正在尝试为 iOS 制作一个简单的程序。该程序将显示带有标记的谷歌地图。我需要添加搜索字符串来过滤标记。但是当我尝试将 TextField 添加到我的应用程序时 - 它因错误“被调用的构造函数不是 const 构造函数”而崩溃。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'src/locations.dart' as locations;
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
...
@override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: []);
return MaterialApp(
home: Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
mapType: _currentMapType,
zoomControlsEnabled: false,
myLocationEnabled: false,
onCameraMove: _onCameraMove,
markers: _rmarkers.values.toSet(),
),
Positioned(
top: 32,
left: 5,
child: Container(
margin: EdgeInsets.all(20),
height: 70,
width: 300,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(15)),
boxShadow: <BoxShadow>[
BoxShadow(
blurRadius: 10,
offset: Offset(5.0, 5.0),
color: Colors.black.withOpacity(0.8))
]),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.search, size: 36.0),
Container(
padding: EdgeInsets.all(3.0),
child: Expanded(
==> HERE child: TextField(
==> THE decoration: InputDecoration(
==> PROBLEM hintText: "What is you looking for?",
),
))),
Icon(Icons.account_circle, size: 36.0),
]),
),
),
],
),
),
);
}
}
上面没有声明任何const。而且我在这里或通过互联网找不到任何想法,这里有什么问题。如果我要改变:
TextField(
decoration: InputDecoration(
hintText: "What is you looking for?",
),
)
经过
Text("What is you looking for?")
一切正常。
解决方案
我找到了问题的根源。Flutter自动生成的代码(我开始新项目时): 文件test/widget_test.dart
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
^^^^^
删除这个const可以解决问题!
推荐阅读
- python - 如何将 cov 函数用于数据集 iris python
- r - 您可以使用 lapply/sapply on 将一个列表的元素应用到另一个列表的相应元素吗?
- c - 用strcmp比较两个字符串时word[0]是什么意思
- ios - 与 Android/Windows 设备相比,iOS 设备上的文档大小增加
- assembly - 如何在大会 8086 中找到星期几
- java - 位图图像文件到 int
- c# - 在 C# 中转换和转换
- excel - 如何让 Power Query/Excel 从同一列中分离和关联两个唯一 ID?
- python - 准确率/召回率图上非常奇怪的形状
- php - 我想通过循环创建数组并从该数组中计算相同的值