在flutter docs中,有一个无状态小部件子类的示例代码,如下所示:
class GreenFrog extends StatelessWidget {
const GreenFrog({ Key key }) : super(key: key);
@override
Widget build(BuildContext context) {
return new Container(color: const Color(0xFF2DBD3A));
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个
class Frog extends StatelessWidget {
const Frog({
Key key,
this.color: const Color(0xFF2DBD3A),
this.child,
}) : super(key: key);
final Color color;
final Widget child;
@override
Widget build(BuildContext context) {
return new Container(color: color, child: child);
}
}
Run Code Online (Sandbox Code Playgroud)
什么是关键,什么时候应该使用这个超级构造函数?好像你有自己的构造函数,你必须有{Key key}为什么?我已经看到了其他没有使用super关键字的例子,所以这就是我的困惑所在.
我是新手扑动/飞镖,所以当我尝试制作应用程序时,我也试着理解为什么事情是某种方式.在flutter文档中,有一个有状态小部件的示例代码,如下所示:
class YellowBird extends StatefulWidget {
const YellowBird({ Key key }) : super(key: key);
@override
_YellowBirdState createState() => new _YellowBirdState();
}
class _YellowBirdState extends State<YellowBird> {
@override
Widget build(BuildContext context) {
return new Container(color: const Color(0xFFFFE306));
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
为什么他们用两个类而不是一个类定义?我猜测State类可以在其他地方使用,所以最好分开.
从我的理解,createState()函数返回一个类型的对象State,所以_YellowBirdState extends State有意义,但为什么YellowBird传递到泛型类State?我的猜测它与Yellowbird扩展StatefulWidget课程有关但不太确定.
我有一个应用程序,登录后路由到"mainapp"页面.此应用程序包含一个底部导航栏,显示相应的按下图标的页面.我想将类型的数据传递Map<String, dynamic>给这些页面,但我遇到了麻烦.此映射是从一个函数生成的,该函数从服务器获取数据,将其保存到共享首选项,然后加载共享首选项并将其作为映射返回(全部包含在内getData()).我想传递这个地图,所以我不必每次都加载共享首选项,但也会在需要时更新此地图以及共享首选项(可能是其中一个页面上的操作).
class MainApp extends StatefulWidget {
@override
_MainAppState createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
Map<String, dynamic> Data;
StartFunc() async {
Data = await getData();
setState(() {});
}
@override
void initState() {
StartFunc();
super.initState();
}
var _pages = [
PageOne(Data:Data),
PageTwo(),
PageThree(),
PageFour(),
PageFive(),
];
int _currentIndex = 0;
onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return _currentIndex == 2
? PageTwo()
: Scaffold(
body: …Run Code Online (Sandbox Code Playgroud) 我正在Golang学习Web开发(初学者)我遇到了一些我玩过的代码,我不太清楚它为什么会起作用,我查看了库的源代码和文档,但我只是有一个模糊的想法,它仍然不是'点击.请注意以下代码:
package main
import (
"fmt"
"net/http"
)
type foo int
func (m foo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Some text")
}
func main() {
var bar foo
http.ListenAndServe(":8080", bar)
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,添加ServeHTTP(w http.ResponseWriter,r*http.Request)作为函数方法,调用处理程序接口(如果我说的正确),现在foo也是类型处理程序.我也明白http.ListenAndServe接受类型处理程序的输入,这是我的变量栏发挥作用的地方.当我运行代码并在浏览器上转到localhost:8080时,我会看到"Some Text".
编辑: 实现接口是正确的术语NOT调用.
题:
这究竟如何运作?如何访问ServeHTTP功能?
我尝试查看库的源代码,但无法准确确定ServeHTTP的工作原理.我找到了这两段代码(不确定这是否适用),这让我觉得它正在实现一个功能,但需要澄清:
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate …Run Code Online (Sandbox Code Playgroud) 我有一个流构建器,它显示来自服务器的“帖子”列表。我已经使用 BLoC 架构来实现这一点。但是出于某种原因,当我切换选项卡并返回时,帖子消失了,我该如何防止帖子消失或让它们重新呈现?以下是我认为相关的一小部分代码,如果需要,我可以添加更多:
选项卡 UI(并非所有代码,包含 BLoC 的文件都在顶部导入):
@override
void initState() {
bloc.fetchMyPosts();
super.initState();
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text("Posts", style: Style.appBarStyle),
bottom: TabBar(
tabs: [
Tab(
text: "My Posts",
),
Tab(
text: "My Other Posts",
),
],
),
),
body: TabBarView(
children: [
Posts(stream: bloc.myPosts), //Stream builder with SliverChildBuilderDelegate
Posts(stream:bloc.myOtherPosts),//Stream builder with SliverChildBuilderDelegate
],
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
流生成器(帖子):
Widget Posts({Stream stream, //Other variables}) {
return StreamBuilder(
stream:stream, …Run Code Online (Sandbox Code Playgroud) 我有一个带有自定义底部导航栏的主屏幕,当单击图标时,它将当前索引设置为单击的图标。但是当点击索引号 2 时,它会执行一个完整的页面路由到另一个页面以添加“帖子”。帖子提交后,pushReplacement 会返回主屏幕(以防止返回)。但是在它路由到主屏幕到索引 0 处的默认屏幕后,返回按钮仍然显示在顶部。单击它后它消失了,但该屏幕上的数据略有不同。我怎样才能解决这个问题?
我的主屏幕上的部分代码:
Widget customNav() {
return Container(
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
//some icons before
IconButton(
icon: Icon(Icons.add_circle),
onPressed: () => Navigator.push(
context,
FadeRoute(
builder: (_) => BlocProvider<AppBloc>(
builder: (_, bloc) => bloc ?? AppBloc(),
onDispose: (_, bloc) => bloc.dispose(),
child: AddPost(userBloc: widget.userBloc)))),
),
//icons after
],
));
}
Run Code Online (Sandbox Code Playgroud)
添加帖子页面:
_submitFunc() async {
// sending it to backend
if (everything is good) {
Navigator.pushReplacement(
context,
FadeRoute(
builder: (_) =>
BlocProvider<AppBloc>(
builder: (_, …Run Code Online (Sandbox Code Playgroud) 我正在解码响应正文,但出现错误:
'List<dynamic>' is not a subtype of type 'List<Example>'
Run Code Online (Sandbox Code Playgroud)
我正在解析json对象的json数组,其中一个字段也是对象列表,并且我怀疑我的问题是由此引起的。我也在使用json_serializable库。下面是我的代码,我省略了一些字段并更改了一些变量名,但是它表示相同的代码:
import 'package:json_annotation/json_annotation.dart';
part 'example_model.g.dart';
@JsonSerializable()
class Example {
(some fields here)
final List<Random> some_urls;
final List<String> file_urls;
const Example({
(some fields here)
this.some_urls,
this.file_urls,
});
factory Example.fromJson(Map<String, dynamic> json) =>
_$ ExampleFromJson(json);
}
@JsonSerializable()
class Random {
final String field_1;
final int field_2;
final int field_3;
final int field_4;
final bool field_5;
constRandom(
{this.field_1, this.field_2, this.field_3, this.field_4, this.field_5});
factory Random.fromJson(Map<String, dynamic> json) => _$RandomFromJson(json);
}
Run Code Online (Sandbox Code Playgroud)
从json_serializable制作的.g dart文件(省略编码部分)中:
Example _$ExampleFromJson(Map<String, dynamic> …Run Code Online (Sandbox Code Playgroud) 我正在使用 flutter_bloc 库来构建我的应用程序。除了 BlocProvider 之外,我还使用了 Repository Provider,因为我将在整个应用程序中广泛使用特定的存储库。但是我在上下文方面遇到了问题。以下是我的代码片段:
main.dart
void main() async {
.......
appRepository _appRepository = AppRepository();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(
BlocProvider(
builder: (context) =>
AuthenticationBloc(appRepository: _appRepository)..dispatch(AppStarted()),
child: App(appRepository: _appRepository,),
),
);
});
}
class App extends StatelessWidget {
............
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (BuildContext context, AuthenticationState state) {
.....
if (state is AuthenticationUnauthenticated) {
return SafeArea(
top: false,
bottom: false,
child: RepositoryProvider(
builder: (context) => _appRepository,
child: LoginPage(firebaseMessaging: _firebaseMessaging),
), …Run Code Online (Sandbox Code Playgroud) 在Angular 4文档中,路由的代码片段如下所示:
导入模块:
import { RouterModule, Routes } from '@angular/router';
Run Code Online (Sandbox Code Playgroud)
路由示例:
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'hero/:id', component: HeroDetailComponent },
{
path: 'heroes',
component: HeroListComponent,
data: { title: 'Heroes List' }
},
{ path: '',
redirectTo: '/heroes',
pathMatch: 'full'
},
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{ enableTracing: true } // <-- debugging purposes only
)
// other imports here
],
...
})
export class AppModule …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Welcome to Flutter',
home: new Scaffold(
appBar: new AppBar(
title: new Text('Welcome to Flutter'),
),
body: new Center(
child: new Text('Hello World'),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我对Buildcontext的理解是它是窗口小部件的“上下文”(如果我错了或进一步阐述,请更正我)。我不明白的是参数“上下文”如何传递到方法中。runApp函数是否提供“上下文”?
我开始学习C++了.我遇到了关于指针的小混乱,特别是NULL指针.根据我的理解,当您声明指针并将其设置为指向值时,您可以执行以下操作:
int var = 10;
int *ptr;
ptr = &var;
*ptr = 20;
Run Code Online (Sandbox Code Playgroud)
这会将var的值更改为20.但是当您设置NULL指针时,您将执行以下操作:
int *ptr = NULL;
Run Code Online (Sandbox Code Playgroud)
这是不是意味着你将值NULL分配给ptr指向的任何变量而不是地址,因为*运算符?我认为一个NULL指针的值(它的地址)为0,所以它从我读到的地方找不到任何地方.这样做会更有意义吗:
int *ptr;
ptr = NULL // or 0?
Run Code Online (Sandbox Code Playgroud)
外行术语的解释将不胜感激,我仍在学习所有的术语,因为我编码和研究,所以我喜欢你使用的任何编程术语的解释.
dart ×8
flutter ×8
angular ×1
bloc ×1
c++ ×1
generics ×1
go ×1
json ×1
mean-stack ×1
null-pointer ×1
typescript ×1