我阅读了有关在 Flutter 应用程序中初始化 SharedPreferences 的其他答案(例如这个),但我想采取另一条路线:在一开始就初始化 SharedPreferences 对象,然后根据需要将其传递给特定的客户端(又名依赖注入)。
我试图使我的主要方法异步,然后使用 await:
void main() async {
var prefs = await SharedPreferences.getInstance();
runApp(MyApp(prefs));
}
Run Code Online (Sandbox Code Playgroud)
直觉上,我预计执行将停止直到prefs
被初始化,然后继续执行runApp
。不幸的是,我得到了一个白屏而不是我的用户界面,所以我想我在这里遗漏了一些东西。
我也尝试使用then
:
void main() async {
SharedPreferences.getInstance().then((prefs) => runApp(MyApp(prefs)));
}
Run Code Online (Sandbox Code Playgroud)
相同的结果:白屏。
有没有办法在 Flutter 中以这种方式初始化 SharedPreference?
Launching lib\main.dart on SM G615F in debug mode...
lib\main.dart:1
Exception in thread "main" java.lang.RuntimeException: Timeout of 120000 reached waiting for exclusive access to file: C:\Users\Me\.gradle\wrapper\dists\gradle-6.7-all\cuy9mc7upwgwgeb72wkcrupxe\gradle-6.7-all.zip
at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:61)
at org.gradle.wrapper.Install.createDist(Install.java:48)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
[!] Gradle threw an error while downloading artifacts from the network. Retrying to download...
Exception in thread "main" java.lang.RuntimeException: Timeout of 120000 reached waiting for exclusive access to file: C:\Users\Shaad\.gradle\wrapper\dists\gradle-6.7-all\cuy9mc7upwgwgeb72wkcrupxe\gradle-6.7-all.zip
at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:61)
at org.gradle.wrapper.Install.createDist(Install.java:48)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
[!] Gradle threw an error while downloading artifacts from …
Run Code Online (Sandbox Code Playgroud) 当我旋转立方体时,立方体的 2 个面是不透明的,而其他面是透明的。
输出:
代码:
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cube',
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: const Cube(),
);
}
}
class Cube extends StatefulWidget {
const Cube({Key? key}) : super(key: key);
@override
_CubeState createState() => _CubeState();
}
class _CubeState extends State<Cube> {
Offset offset = Offset.zero;
@override
Widget build(BuildContext context) {
return Scaffold( …
Run Code Online (Sandbox Code Playgroud) 我的应用程序的最低 SDK 为 16 并且我正在使用AppCompatActivity
,因此我需要将其Theme.AppCompat.x
用作我的基本主题。
我知道如果我调用getSupportActionBar().setElevation(0)
,我可以删除每个活动的操作栏下的阴影。
我想要做的是通过使用主题来应用这种效果。有什么想法可以实现这一目标吗?
我的主要风格看起来像这样,应用程序使用@style/AppTheme
.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primary_dark</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我有一个包含以下值的列表:
List<Map<String, dynamic>> arrayTest = [{'id':'1','name':'test1'},{'id':'2','name':'test2'},{'id':'3','name':'test3'},{'id':'4','name':'test5'}];
Run Code Online (Sandbox Code Playgroud)
我想id
将 arrayTest 变量中的所有值都放入另一个List<dynamic>
类型列表中。
我是颤振的新手,我已经尝试过这个.map()
功能,但不知道如何做到这一点。
我尝试使用 Homebrew ( https://formulae.brew.sh/formula/php ) 在 macOS 上安装 PHP,但每次都会出现以下错误:
Warning: No available formula or cask with the name "php".
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.
Run Code Online (Sandbox Code Playgroud)
使用时brew search php
我只能找到phpstorm和eclipse-php。为什么我无法使用 Homebrew 在 macOS 上找到或安装 PHP?提前致谢!
旁注:我已经将 Homebrew 更新到最新版本并在 GitHub 上检查了公式,上次提交是 6 天前https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/php.rb
我想对大量数据进行分页。我一直在Flutter中寻找分页,但它们都显示使用加载更多或延迟加载的方式,但我想逐页加载数据。那么,如果我有 100 个数据列表,第一页上将有 1-10 的数据列表,第二页:11-20,...有任何资源可供我执行此操作吗?