在阅读 dart 代码时,我经常看到一些仅使用下划线 _ 参数调用的函数。它让我烦恼了一段时间,并且由于 flutter 改进了它的分析消息,我有一些线索......但我觉得我并没有真正掌握这个概念:-(
昨天我写了以下内容进行测试:
when(mockDevice.getLocalPath()).thenAnswer(() async => fileFolder);
Run Code Online (Sandbox Code Playgroud)
并得到以下分析
错误:无法将参数类型“Future Function()”分配给参数类型“Future Function(Invocation)”。
添加下划线时,它工作正常。
when(mockDevice.getLocalPath()).thenAnswer((_) async => fileFolder);
Run Code Online (Sandbox Code Playgroud)
我遇到的最可怕的例子来自@remi rousselet 编写的 provider package
builder: (_, counter, __) => Translations(counter.value),
Run Code Online (Sandbox Code Playgroud)
它来自提供者示例:
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(builder: (_) => Counter()),
ProxyProvider<Counter, Translations>(
builder: (_, counter, __) => Translations(counter.value),
),
],
child: Foo(),
);
}
class Translations {
const Translations(this._value);
final int _value;
String get title => 'You clicked $_value times';
}
Run Code Online (Sandbox Code Playgroud) flutter_web 文档说 dart:svg 已移植,但是您如何将其与 flutter 资产/小部件系统一起使用?
我想测试一些小部件的属性,但我找不到简单的方法。
这是一个带有密码字段的简单示例,我如何检查obviousText是否设置为 true ?
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
const darkBlue = Color.fromARGB(255, 18, 32, 47);
Future<void> main() async {
testWidgets('Wheelio logo appear on the login screen',
(WidgetTester tester) async {
final Key _formKey = GlobalKey<FormState>();
final TextEditingController passwordController = TextEditingController();
const Key _passwordKey = Key('PASSWORD_KEY');
final Finder passwordField = find.byKey(_passwordKey);
await tester.pumpWidget(MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Form(
key: _formKey,
child: TextFormField(
key: _passwordKey,
obscureText: true,
controller: passwordController,
),
),
),
),
));
await tester.pump(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试用flutter和firebase构建我的第一个移动应用程序.当我尝试显示和存储照片时,我遇到以下问题:
错误:无法将参数类型"Future"分配给参数类型"File".(argument_type_not_assignable在[whereassistant] lib/main.dart:85)
我应该做一些铸造,但我不理解hox做得恰当.
这是我的Future文件声明:
Future<File> _imageFile;
Run Code Online (Sandbox Code Playgroud)
我正在拍摄一张显示在屏幕上的照片:
setState(() {
_imageFile = ImagePicker.pickImage(source: source);
});
Run Code Online (Sandbox Code Playgroud)
但是在尝试将照片发送到Firebase时出现错误:
final StorageUploadTask uploadTask = ref.put(_imageFile);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
Run Code Online (Sandbox Code Playgroud)
这是我基于代码示例使用的类:
class _MyHomePageState extends State<MyHomePage> {
Future<File> _imageFile;
void _onImageButtonPressed(ImageSource source) async {
GoogleSignIn _googleSignIn = new GoogleSignIn();
var account = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await account.authentication;
final FirebaseUser user = await _auth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser …Run Code Online (Sandbox Code Playgroud) 从flutter 1.9开始,flutter_web在flutter中合并。我正在尝试在chrome浏览器中运行初始flutter应用程序,但失败。操作系统是Ubuntu 19.04
我成功使用的命令:flutter upgrade
颤动创建test_1_9
cd test_1_9
扑动
该命令不起作用:
颤振运行-d铬
=>未找到名称或ID与“ chrome”匹配的设备
我尝试使用google-chrome作为设备名称,结果相同
[?] Flutter (Channel master, v1.10.2-pre.38, on Linux, locale fr_FR.UTF-8)
• Flutter version 1.10.2-pre.38 at /home/rbarbe/programes/flutter
• Framework revision e6ae95c4ce (il y a 18 heures), 2019-09-11 07:46:57 -0700
• Engine revision 7ea9884ab0
• Dart version 2.5.0 (build 2.5.0-dev.4.0 be66176534)
[?] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /home/rbarbe/Android/Sdk
• Android NDK location not configured (optional; useful for native profiling …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用颤振进行一些 TDD,并且在测试运行时,如果按钮在抽屉中,则点击按钮不起作用。该按钮对于普通用户来说效果很好。
在下面的示例中,我们按下两个按钮,在控制台中打印一条消息。以下是操作:
找到并点击脚手架中的按钮:确定
打开抽屉:确定
找到
抽屉中的按钮:确定点击抽屉按钮:什么也没有发生
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Test that drawer is apparing and we can click on button',
(WidgetTester tester) async {
final scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(new MaterialApp(
title: 'Where Assistant',
home: Scaffold(
key: scaffoldKey,
body: Column(
children: <Widget>[
Text('test text'),
RaisedButton(
onPressed: () {
print('OK on main screen');
},
child: Icon(Icons.access_alarm),
),
],
),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// …Run Code Online (Sandbox Code Playgroud) 为了创建一个 android/web 应用程序,我想在特定平台上运行 flutter 测试。当我指定“chrome”时,我得到“没有运行测试”。当然,当我执行“flutter run -d chrome”时,应用程序正在运行
\n\n使用 flutter 启动应用程序很容易重现该问题。这是我在 Linux 上所做的:
\n\nmkdir chrome_test\ncd chrome_test\ncreate .\nflutter test test/\nRun Code Online (Sandbox Code Playgroud)\n\n=> 所有测试都通过了!
\n\n当我在 test/widget_test.dart\n=> 开头添加 @TestOn("vm") 时,所有测试都通过了!
\n\n当我在 test/widget_test.dart\n=> 开头添加 @TestOn("chrome") 时,没有运行任何测试。
\n\n[\xe2\x9c\x93] Flutter (Channel master, v1.10.6-pre.39, on Linux, locale fr_FR.UTF-8)\n \xe2\x80\xa2 Flutter version 1.10.6-pre.39 at /home/rbarbe/programes/flutter\n \xe2\x80\xa2 Framework revision 4815b26d71 (il y a 7 jours), 2019-09-24 00:21:44 -0700\n \xe2\x80\xa2 Engine revision 953ac71749\n \xe2\x80\xa2 Dart version 2.6.0 (build 2.6.0-dev.1.0 d53d355c6c)\n\n[\xe2\x9c\x93] Android toolchain - develop for …Run Code Online (Sandbox Code Playgroud) 我在 flutter 应用程序中使用 json 文件并在 firestore 中记录此 json 失败。
我的 json 位于 jsonCloudContent 中。以下不起作用:
Firestore.instance.collection('toto').document().setData(jsonCloudContent);
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 json.encode(jsonCloudContent) 中的字符串时,它工作正常:
Firestore.instance.collection('userStuffLists').document().setData({"apiVersion":2,"userId":"unknown","googleId":"unknown","list":[{"userI...
Run Code Online (Sandbox Code Playgroud)
这是 json 文件的示例:https://jsoneditoronline.org/? id=3ac25ef1743047e08467cccbad031d5e
上传数据的功能:
Future<bool> uploadUserStuffList() async {
Map<String, dynamic> jsonCloudContent = StuffList(
list: list,
apiVersion: apiVersion,
userId: userId,
googleId: googleId,
listVersion: listVersion)
.toJson();
try {
Firestore.instance.collection('toto').document().setData(jsonCloudContent);
} catch (e) {
debugPrint(e);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
在 Intellij 检查器中,格式看起来不错,但以防万一,以下是对象定义:
class Stuff {
final String description;
final List<String> imagePath;
final String userId;
// TODO : put unknown in the key_strings …Run Code Online (Sandbox Code Playgroud)