小编use*_*004的帖子

在 dispose() 方法中使用 Provider.of(context) 调用方法会导致“查找停用的小部件的祖先是不安全的”。

当我尝试在 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)

dart flutter flutter-layout

18
推荐指数
1
解决办法
3236
查看次数

Flutter 压缩图像的最佳方式

我尝试在使用 flutter image_picker 包上传到存储之前压缩图像。我找到了三种方法来做到这一点,但我不确定哪种方法是最好的。

  1. 使用 image_picker 包的 imageQuality 参数

图像选择器

  1. 使用 flutter_image_compress 包

flutter_image_compress

  1. 使用 flutter_native_image 包

flutter_native_image

这些选项之间有什么区别吗?

感谢任何帮助和解释。

dart flutter

16
推荐指数
3
解决办法
2万
查看次数

在 initState 中为变量赋值与在 Flutter StatefulWidget 中不赋值有什么区别吗?

我在许多示例代码中看到了两种使用 StatefulWidget 声明变量的方法。

  1. 用值初始化变量(firstCase
  2. 初始化没有值的变量并分配给 initState ( secondCase )内的值

这些有什么区别吗?或者哪一个在实践中是更好的代码?

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)

dart flutter

11
推荐指数
1
解决办法
877
查看次数

使用 AVPlayer、AVPlayerItem 和 AVPlayerAsset 之间有什么区别吗?

我找到了通过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)

以上这些有什么区别吗?

ios avplayer avasset swift avplayeritem

9
推荐指数
1
解决办法
2770
查看次数

如何在 SwiftUI 中删除 List 和 ScrollView 的底部填充

我想删除底部填充,即红色空间之间的空白。有什么办法可以实现吗?

在此输入图像描述

测试代码:

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)

swift swiftui swiftui-list

6
推荐指数
1
解决办法
9915
查看次数

如何将 .share() 与主题一起使用 [Swift Couple]

如果我有一个主题并从下面的许多地方订阅它,我应该使用 .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)

swift combine

1
推荐指数
1
解决办法
1253
查看次数

Firestore - 创建文档引用算作“读”还是“写”?

来自firestore 文档

在某些情况下,使用自动生成的 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?

javascript firebase google-cloud-firestore

1
推荐指数
1
解决办法
552
查看次数