我有一个简单的表单,带有一个按钮来计算表格.我认为最好按下按钮开始计算动作并将变量传递给哑函数,而不是让函数知道它不应该知道的文本字段.我可以这样做,还是我的计算功能需要访问我的TextFields?
new Container(
color: Colors.grey.shade300,
padding: new EdgeInsets.all(15.0),
child: new Column(
children: <Widget>[
new TextField(
controller: _ageController,
decoration: new InputDecoration(
icon: new Icon(Icons.person_outline), labelText: "Age"),
),
new TextField(
controller: _heightController,
decoration: new InputDecoration(
icon: new Icon(Icons.insert_chart),
labelText: "Height (in feet)"),
),
new TextField(
controller: _weightController,
decoration: new InputDecoration(
icon: new Icon(Icons.line_weight),
labelText: "Weight (in lbs)"),
),
new Padding(padding: new EdgeInsets.only(top: 20.0)),
new RaisedButton(
onPressed: calculate(1, 2),
color: Colors.pinkAccent,
child: new Text(
"Calculate",
style: new TextStyle(color: Colors.white),
),
)
],
), …Run Code Online (Sandbox Code Playgroud) 我正在尝试找到一种方法,以在FutureBuilder完成加载时指示周围的类。RefreshIndicator将a Future作为参数,并在Future完成时停止显示刷新指示符。我无法访问传递给的完全相同的Future变量FutureBuilder,尤其是当它们位于两个单独的类中时,除非我可以传递对一个的引用,而当它在另一个类中完成时,我就会知道...
我在关于 C# 的类似问题的评论中读到扩展方法只能从实例中调用。Dart 也是如此吗?我正在尝试向将这样调用的Platform 类添加一个 getter 。Platform.isDesktop. 然而,这仅在调用类的实例时有效,即Platform().isDesktop,即使将实例方法声明为静态。为什么我们不能添加静态成员?
代码:
extension on Platform {
bool get isMobile => Platform.isAndroid || Platform.isIOS;
bool get isDesktop => Platform.isWindows || Platform.isMacOS || Platform.isLinux;
}
Run Code Online (Sandbox Code Playgroud)
我正在调用的of功能InheritedWidget,但是当如上所示,当我的窗口小部件位于树的顶部时,我的函数将返回null。呼叫来自推送到导航器堆栈的页面,而不是该页面。有人知道为什么吗?我的InheritedWidget代码如下。
class LiveKHProvider extends InheritedWidget {
final LiveKHBloc liveKHBloc = LiveKHBloc();
@override
bool updateShouldNotify(InheritedWidget oldWidget) => oldWidget != this;
static LiveKHBloc of(BuildContext context) {
var inheritFromWidgetOfExactType =
context.inheritFromWidgetOfExactType(LiveKHProvider); // to clearly see what’s returning null.
//This is where it returns null,
//so the below line is executed on a null object.
return (inheritFromWidgetOfExactType as LiveKHProvider).liveKHBloc;
}
LiveKHProvider({Key key, Widget child}) : super(key: key, child: child);
}
Run Code Online (Sandbox Code Playgroud) 打印语句或其下面的任何内容均未运行,并且错误消息指出该问题是以开头的最后一行var time。我还验证了这earthquakes是一个growableList,这意味着它earthquakes[0]应该没有问题地运行,但事实并非如此……我在做什么错?让我知道这个问题是否需要进一步说明,我会提供。链接到错误的gif链接到GitHub上的代码
我的代码中有问题的部分如下。在第43行报告错误。
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
class Quake extends StatefulWidget {
var _data;
Quake(this._data);
@override
State<StatefulWidget> createState() => new QuakeState(_data);
}
class QuakeState extends State<Quake> {
// https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
// "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson";
var _data;
QuakeState(this._data);
@override
Widget build(BuildContext context) {
// debugPrint(_data['features'].runtimeType.toString());
List earthquakes = _data['features'];
return new Scaffold(
appBar: new AppBar(
title: new Text("Quakes - USGS All Earthquakes"),
backgroundColor: Colors.red,
centerTitle: true,
), …Run Code Online (Sandbox Code Playgroud) 我的InheritedWidget应用程序的根目录,基本上是我第一个构建函数的顶部Scaffold.我可以使用.of(context)第一页的所有子窗口小部件中的函数来引用窗口小部件.如果我在Navigator堆栈上推送另一个页面,则InheritedWidget.of()调用失败,因为我认为它应该.有没有办法共享这个小部件的访问权限?我试图避免不必要地将小部件传递到树上.ScopedModel具有相同的限制.我正在考虑将上下文变量传递到树下,但这听起来不对,它也没有解决问题.
通常 a 的密钥Widget是var key = GlobalKey<WidgetState>私有TextFormFieldState的。由于我只有一种文本形式,因此我避免将其包装在Form. 能做到吗?
说我的项目结构如下。
contact_book <---(App)
??? bin
? ??? contact_book.dart
??? contact_book.iml
??? lib
? ??? address.dart
? ??? email.dart
? ??? field.dart
? ??? functions.dart
? ??? person.dart
? ??? phone_number.dart
??? pubspec.lock
??? pubspec.yaml
functions <---(Package)
??? bin
? ??? lib
? ??? functions.dart
??? functions.iml
??? pubspec.lock
??? pubspec.yaml
Run Code Online (Sandbox Code Playgroud)
两个文件夹都在同一个目录下。如何调用属于其他包中 .dart 文件一部分的函数?从阅读飞镖网站来看,这似乎是可能的。这样我就可以编写自己的函数并在不同的项目中使用它们。我是不是刚刚读错了,必须将文件复制到我的程序中?
链接:创建库包 - Dart
我正在使用图像选择器插件来选择图像。我想在选取图像后立即导航到新屏幕,但它不起作用。我收到一条错误,指出当前小部件树中不存在上下文。
下面是我的代码。
pickImage(BuildContext context) async {
File pickedImage = await ImagePicker.pickImage(source: ImageSource.camera);
if (pickedImage != null) {
print(pickedImage.path);
if (this.mounted) {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ViewStory(
localImagePath: pickedImage.path,
),
),
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
像这样调用该函数:
IconButton(
onPressed: () => pickImage(context),
icon: Icon(
Icons.camera_alt,
color: CustomColors.primary,
size: 100,
),
),
Run Code Online (Sandbox Code Playgroud)
以下是我收到的错误:
FlutterError(查找已停用的小部件的祖先是不安全的。此时小部件的元素树的状态不再稳定。要在其 dispose() 方法中安全地引用小部件的祖先,请通过调用inheritFromWidgetOfExactType(保存对祖先的引用) )在小部件的 didChangeDependency() 方法中。)
我想Future.wait()在一些网络调用上运行,以便它们可以同时运行。我想进一步加快速度,在结果可用时返回结果,以便我可以开始显示它们。我认为只需对调用进行一个小修改Future.wait就可以了,但不幸的是,它在内部使用私有类。有更好的方法来实现这一点吗?