我目前正在測驗并嘗試 Flutter 應用程式的新設定頁面。我是 Flutter 的新手,所以我跟著教程一起學習,但是我收到了一個錯誤,教程視頻沒有通過 VS Code 顯示:
未定義命名引數“用戶名”。嘗試將名稱更正為現有命名引數的名稱,或使用名稱 'username'.dartundefined_named_pa??rameter 定義命名引數
和
引數“用戶名”因其型別而不能具有“空”值,但隱含的默認值為“空”。嘗試添加顯式的非“空”默認值或“必需”修飾符。dartmissing_default_value_for_parameter
哪個 VS Code 在兩個檔案(分別為第 217 行和第 8 行)中用紅色強調了“用戶名”變數。我正在測驗變數并在線查找解決方案,但我找不到我理解的非常簡單的解決方案。如果這是一個非常簡單的問題,我很抱歉,但我是 Flutter 的新手,不明白出了什么問題。感謝您的時間。
代碼的總體目標是保存用戶名并將其顯示在其他頁面中。
main.dart 檔案(第 148 - 222 行)
// Settings Page & Account Information
class Settings extends StatelessWidget {
Settings({Key? key}) : super(key: key);
final _usernameController = TextEditingController();
@override
Widget build(BuildContext context) {
final theme = MediaQuery.of(context).platformBrightness == Brightness.dark
? 'DarkTheme'
: 'LightTheme';
return Scaffold(
appBar: AppBar(title: const Text('Settings'), actions: <Widget>[
IconButton(
onPressed: () async {
_saveSettings;
},
icon: const Icon(Icons.save),
tooltip: 'Save Settings')
]),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
child: Column(
children: [
Column(
// Account
children: [
const Padding(
padding: EdgeInsets.fromLTRB(0, 12, 0, 0),
child: Text('Account Information',
style: TextStyle(
fontSize: 17.0,
))),
Padding(
padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
child: TextField(
controller: _usernameController,
inputFormatters: [LengthLimitingTextInputFormatter(25)],
decoration: InputDecoration(
hintText: 'Username',
labelText: 'Username',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0)),
),
),
),
],
),
Container(
child: Column(
// App Settings
children: [
// SwitchListTile(value: DarkMode, onChanged: Light => Dark => Light)
// ChangeThemeButtonWidget(),
TextButton(
onPressed: _saveSettings,
child: const Text('Save Settings'))
],
),
),
],
),
)));
}
void _saveSettings() {
final newSettings = Settings(
username: _usernameController.text,
);
print(newSettings);
}
}
saved_data.dart 檔案
import 'package:shared_preferences/shared_preferences.dart';
import 'package:bit/main.dart';
class Settings {
final String username;
Settings({
this.username,
});
}
uj5u.com熱心網友回復:
您應該考慮為這兩個類使用不同的名稱。此外,對于第二個設定類,訊息很清楚:
嘗試添加顯式的非“空”默認值或“必需”修飾符。dartmissing_default_value_for_parameter
因此,設定變為:
class SettingsModel {
final String username;
SettingsModel({
required this.username,
});
}
并且,設定應該是:
void _saveSettings() {
final newSettings = SettingsModel(
username: _usernameController.text,
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/430633.html
