参数类型 'Future<SharedPreferences>' 无法分配给参数类型 'SharedPreferences'
The argument type 'Future<SharedPreferences>' can't be assigned to the parameter type 'SharedPreferences'
我想在应用程序启动时访问共享首选项,并希望通过将同一个对象传递给 类 在整个应用程序中使用它。我收到以下错误:
The argument type 'Future' can't be assigned to the
parameter type 'SharedPreferences'.
main.dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:application/layouts/ScreenOne.dart';
import 'package:application/layouts/ScreenTwo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
sharedPreferences() async {
return await SharedPreferences.getInstance();
}
final preferences = SharedPreferences.getInstance();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
);
}
}
我无法解决这个错误。有什么我做错或遗漏的吗?谢谢!!!
首先,您定义了函数 sharedPreferences()
但稍后在代码中没有使用它 - 只需将其删除。
此外,根据文档 SharedPreferences.getInstance()
returns Future<SharedPreferences>
而不是 SharedPreferences
,因此您会收到以下错误。您可以通过在 main 方法中获取 SharedPreferences
实例然后使用构造函数注入将 preferences
对象提供给 MyApp
:
来解决此问题
Future<void> main() async { // <-- Notice the updated return type and async
final preferences = await SharedPreferences.getInstance(); // <-- Get SharedPreferences instance
runApp(
MyApp(preferences: preferences), // <-- Inject (pass) SharedPreferences object to MyApp
);
}
class MyApp extends StatelessWidget {
final SharedPreferences preferences;
const MyApp({
required this.preferences,
})
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
);
}
}
我想在应用程序启动时访问共享首选项,并希望通过将同一个对象传递给 类 在整个应用程序中使用它。我收到以下错误:
The argument type 'Future' can't be assigned to the parameter type 'SharedPreferences'.
main.dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:application/layouts/ScreenOne.dart';
import 'package:application/layouts/ScreenTwo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
sharedPreferences() async {
return await SharedPreferences.getInstance();
}
final preferences = SharedPreferences.getInstance();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
);
}
}
我无法解决这个错误。有什么我做错或遗漏的吗?谢谢!!!
首先,您定义了函数 sharedPreferences()
但稍后在代码中没有使用它 - 只需将其删除。
此外,根据文档 SharedPreferences.getInstance()
returns Future<SharedPreferences>
而不是 SharedPreferences
,因此您会收到以下错误。您可以通过在 main 方法中获取 SharedPreferences
实例然后使用构造函数注入将 preferences
对象提供给 MyApp
:
Future<void> main() async { // <-- Notice the updated return type and async
final preferences = await SharedPreferences.getInstance(); // <-- Get SharedPreferences instance
runApp(
MyApp(preferences: preferences), // <-- Inject (pass) SharedPreferences object to MyApp
);
}
class MyApp extends StatelessWidget {
final SharedPreferences preferences;
const MyApp({
required this.preferences,
})
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
);
}
}