我正在为应用程序使用 Flutter 和 Firebase Storage。我试图创建一个函数来知道文件是否存在:
Future<bool> exists(String path) async {
bool _exists;
StorageReference storageReference = FirebaseStorage.instance.ref().child(path);
try {
await storageReference.getDownloadURL();
_exists = true;
} catch (exception) {
_exists = false;
}
return _exists;
}
Run Code Online (Sandbox Code Playgroud)
它正在工作,但是当文件不存在时,它仍然记录错误:
I/System.out(12012): (HTTPLog)-Static: isSBSettingEnabled false
E/StorageException(12012): StorageException has occurred.
E/StorageException(12012): Object does not exist at location.
E/StorageException(12012): Code: -13010 HttpResult: 404
E/StorageException(12012): { "error": { "code": 404, "message": "Not Found. Could not get object", "status": "GET_OBJECT" }}
E/StorageException(12012): java.io.IOException: { "error": { "code": 404, "message": "Not …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的 Flutter 应用程序创建测试。简单的例子:
class MyWidget extends StatelessWidget {
@override
build(BuildContext context) {
return MySecondWidget();
}
}
Run Code Online (Sandbox Code Playgroud)
我想验证它MyWidget实际上是在MySecondWidget没有构建的情况下调用MySecondWidget。
void main() {
testWidgets('It should call MySecondWidget', (WidgetTester tester) async {
await tester.pumpWidget(MyWidget());
expect(find.byType(MySecondWidget), findsOneWidget);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,这将不起作用,因为MySecondWidget需要一些特定且复杂的设置(例如 API 密钥、...中的值Provider)。我想要的是“模拟” MySecondWidget为空Container(例如),以便在测试期间不会引发任何错误。
我怎么能做这样的事情?
我正在开发一个 flutter web 项目,我有一个小部件,我想编写测试。该小部件正在使用 ,MouseRegion当用户将其悬停或未悬停时,该小部件会执行一些操作。
举个例子:
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool isHovered = false;
@override
Widget build(BuildContext context) {
return MouseRegion(
onExit: (_) {
setState(() {
isHovered = false;
});
},
onEnter: (_) {
setState(() {
isHovered = true;
});
},
child: Container(
color: isHovered ? Colors.blue : Colors.red,
height: 50,
width: 50,
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以编写一个小部件测试来测试我的容器是红色的:
testWidgets('MyWidget should be red by default', (WidgetTester tester) …Run Code Online (Sandbox Code Playgroud) 我有一个类似于以下内容的小部件测试:
testWidgets('Widget test', (WidgetTester tester) async {
provideMockedNetworkImages(() async {
final widget = MyWidget();
await WidgetTestFunctions.pumpWidgetTest(
tester,
widget,
);
// ....
await tester.tap(find.byType(MyWidget));
await tester.pump(new Duration(milliseconds: 3000));
// ....
expect(widget.value, myValue);
});
});
Run Code Online (Sandbox Code Playgroud)
以及小部件 on-tap 方法的以下实现:
_onButtonPressed() async {
await animationController.forward();
setState(() {
// ...
// Calls method that changes the widget value.
});
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,animationController.forward()在测试中调用该方法后,该setState部分未执行。我应该如何等待这个方法正确完成?在应用程序运行时,这部分代码被正确调用。
似乎await tester.pump(new Duration(milliseconds: 3000));工作不正常,动画的持续时间为 1500 毫秒,并且您可以看到泵持续时间是双倍的。
我正在进行一个 Flutter 项目,我们正在使用build_runner.
我们正在使用多个构建器,并且运行它们都需要很长时间。我们的文件如下所示:
targets:
$default:
builders:
package1|builder1:
# ... options package1
package2|builder2:
# ... options package2
package3|builder3:
# ... options package3
# ... A bunch of other packages
packageN|builderN:
# ... options packageN
Run Code Online (Sandbox Code Playgroud)
现在假设我在项目中应用了一些更改,并且需要运行构建器。但我知道,只有一些构建器需要运行,而不需要运行其他构建器(比方说builder2和builder3)。
现在,当我跑步时
flutter pub run build_runner build --delete-conflicting-outputs
Run Code Online (Sandbox Code Playgroud)
它正在运行所有构建器,并且需要很长时间。
有没有办法使用命令行运行特定的构建器?
我有这个片段:
final countProvider = StateProvider<int>((ref) {
return 0;
});
class CountWidget extends ConsumerWidget {
const CountWidget();
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(countProvider);
return Column(
children: [
Text(count.toString()),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
ref.read(countProvider.notifier).state++;
},
),
],
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个非常简化的代码,但其想法是它使用状态提供程序。
我想编写一个测试并验证在执行某些操作后,提供程序处于特定状态(不依赖 UI,这里我可以使用find.text(),但我的状态可能要复杂得多)。
我想在抽出我的小部件后在测试中访问模型:
await tester.pumpWidget(const CountWidget());
await tester.tap();
await tester.pump();
// ... Some other actions.
final currentCountState = // ?
expect(currentCountState, 3); // For example.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在尝试编写一个颤振测试并模拟双选项卡。但我无法找到办法。
这是我现在所做的:
void main() {
testWidgets('It should trigger onDoubleTap', (tester) async {
await tester.pumpWidget(MaterialApp(
home: GestureDetector(
child: const Text('button'),
onDoubleTap: () {
print('double tapped');
},
),
));
await tester.pumpAndSettle();
await tester.tap(find.text('button')); // <- Tried with tester.press too
await tester.tap(find.text('button')); // <- Tried with tester.press too
await tester.pumpAndSettle();
});
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我得到的是:
00:03 +1: All tests passed!
Run Code Online (Sandbox Code Playgroud)
double tapped但我在控制台中没有看到任何内容。
如何触发双击?
gesturedetector dart flutter flutter-test single-vs-double-tap
使用provider,Providers 的作用域位于小部件树中。只有小部件的子Provider级可以访问其模型。
它在 的 doc 中说riverpod:
允许在多个位置轻松访问该状态。提供者完全替代了单例、服务定位器、依赖注入或 InheritedWidgets 等模式。
* 这里的“ Providers”指的是
Provider包的类riverpod。
这个provider包是围绕 s 的简化/包装/API InheritedWidget,所以我想 s 可以实现的功能provider也可以通过 s 实现riverpod。
但我无法找出如何做到这一点。
这是我正在尝试迁移的一个小例子。
class Counter extends ValueNotifier<int> {
Counter(): super(0);
}
class MyWidget extends StatelessWidget {
const MyWidget();
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Counter>(
create: (_) => Counter();
child: Builder(
builder: (context) {
return Row( …Run Code Online (Sandbox Code Playgroud) 我正在实现 VSCode 扩展。我按照此链接设置了该项目。
它生成一个带有src/test/runTest.ts文件的启动项目:
import * as path from 'path';
import { runTests } from '@vscode/test-electron';
async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to …Run Code Online (Sandbox Code Playgroud) 我在欢迎屏幕中实现了延迟动画,但在我的 flutter 应用程序中出现以下错误。如果我的代码中有错误,请告诉我,我可以纠正并解决此问题。
错误是:
E/flutter (11565): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)]
Unhandled Exception:
'package:flutter/src/animation/animation_controller.dart': Failed
assertion: line 455 pos 7: '_ticker != null':
AnimationController.forward() called after
AnimationController.dispose()
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
E/flutter (11565): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)]
Unhandled Exception:
'package:flutter/src/animation/animation_controller.dart': Failed
assertion: line 455 pos 7: '_ticker != null':
AnimationController.forward() called after
AnimationController.dispose()
Run Code Online (Sandbox Code Playgroud)