当我尝试在 State dispose 方法中调用一个方法时,如下所示。
@override
void dispose() {
Provider.of<AppProvider>(context, listen: false).close();
super.dispose();
}
Run Code Online (Sandbox Code Playgroud)
我懂了。
The following assertion was thrown while finalizing the widget tree:
Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
When the exception was thrown, this was the stack …Run Code Online (Sandbox Code Playgroud) 我尝试在使用 flutter image_picker 包上传到存储之前压缩图像。我找到了三种方法来做到这一点,但我不确定哪种方法是最好的。
这些选项之间有什么区别吗?
感谢任何帮助和解释。
我在许多示例代码中看到了两种使用 StatefulWidget 声明变量的方法。
这些有什么区别吗?或者哪一个在实践中是更好的代码?
class Sample extends StatefulWidget {
Sample({Key key}) : super(key: key);
@override
_SampleState createState() => _SampleState();
}
class _SampleState extends State<Sample> {
bool firstCase = false;
bool secondCase;
@override
void initState() {
secondCase = false;
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
child: child,
);
}
}
Run Code Online (Sandbox Code Playgroud) 我找到了通过URL播放视频的三种方式。
let url = "some url"
// first way
AVPlayer(url: url)
// second way
let playerItem = AVPlayerItem(url: url)
AVPlayer(playerItem: playerItem)
// third way
let asset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
AVPlayer(playerItem: playerItem)
Run Code Online (Sandbox Code Playgroud)
以上这些有什么区别吗?
我想删除底部填充,即红色空间之间的空白。有什么办法可以实现吗?
测试代码:
struct ContentView: View {
var body: some View {
return NavigationView {
VStack {
// the same result with using List instead of ScrollView
ScrollView {
ForEach(1..<100) { index in
HStack {
Spacer()
Text("\(index)")
Spacer()
}
}
}.background(Color.red)
HStack {
Spacer()
Text("Test")
Spacer()
}
.background(Color.red)
}
.navigationBarTitle(Text("Test"), displayMode: .inline)
}
}
}
Run Code Online (Sandbox Code Playgroud) 如果我有一个主题并从下面的许多地方订阅它,我应该使用 .share() 吗?或者 .share() 在这种情况下什么也不做?
let sampleSubject = CurrentValueSubject<String, Never>("")
sampleSubject.sink { print($0) }
sampleSubject.sink { print($0) }
sampleSubject.sink { print($0) }
sampleSubject.sink { print($0) }
Run Code Online (Sandbox Code Playgroud)
let sharedSampleSubject = sampleSubject.share()
sharedSampleSubject.sink { print($0) }
sharedSampleSubject.sink { print($0) }
sharedSampleSubject.sink { print($0) }
sharedSampleSubject.sink { print($0) }
Run Code Online (Sandbox Code Playgroud) 在某些情况下,使用自动生成的 ID 创建文档引用然后在以后使用该引用可能会很有用。对于此用例,您可以调用 doc()。
import { collection, doc, setDoc } from "firebase/firestore";
// Add a new document with a generated id
const newCityRef = doc(collection(db, "cities"));
// later...
await setDoc(newCityRef, data);
Run Code Online (Sandbox Code Playgroud)
在幕后, .add(...) 和 .doc().set(...) 完全等效,因此您可以使用更方便的那个。
我的问题是,const newCityRef = doc(collection(db, "cities"));在不使用的情况下,这确实算作“读”或“写”吗setDoc(newCityRef, data)?
假设我生成 100 个文档引用但根本不保存它们,我的“读”或“写”计数是 0 或 100?
dart ×3
flutter ×3
swift ×3
avasset ×1
avplayer ×1
avplayeritem ×1
combine ×1
firebase ×1
ios ×1
javascript ×1
swiftui ×1
swiftui-list ×1