在 Flutter 中,为了在我们自己的包的 lib 目录中导入库,我们是否应该使用相对导入
import 'foo.dart'
Run Code Online (Sandbox Code Playgroud)
或包导入?
import 'package:my_app/lib/src/foo.dart'
Run Code Online (Sandbox Code Playgroud)
Dart 指南提倡使用相对导入:
在您自己的包的 lib 目录中导入库时,首选相对路径。
- 始终使用包导入。例如:
import 'package:my_app/my_code.dart';
除了简洁之外还有区别吗?为什么包导入会减少相对导入的错误?
而不是写:
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
Run Code Online (Sandbox Code Playgroud)
导出十几个文件,我想写:
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'
show
PlatformAlertDialog,
PlatformCircularProgressIndicator,
PlatformDialogAction,
PlatformText,
showPlatformDialog;
Run Code Online (Sandbox Code Playgroud)
因为我只使用这些组件。然而,这很乏味(让我想起 Typescript 无穷无尽的导入)并且违背了 Dart 的简洁原则。
在 VSCode 中导入片段使用第一种解决方案,但有什么显着差异,例如在性能方面?有什么好的做法吗?我在官方指南中找不到任何内容。
我使用简单的form-field input组件,如下面的代码
<mat-form-field class="example-form-field" >
<input matInput type="search" placeholder="Search" >
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
在输入默认输入的输入时,占位符将在上方.
如何在输入到输入字段时隐藏占位符?
我正在尝试在Firestore集合上按模式执行过滤器。例如,在我的Firestore数据库中,我有一个名为的品牌adidas。用户将具有搜索输入,其中键入“ adi”,“ adid”,“ adida”或“ adidas”将返回adidas文档。我指出了几种解决方案:
1.获取所有文档并执行前端过滤器
var brands = db.collection("brands");
filteredBrands = brands.filter((br) => br.name.includes("pattern"));
Run Code Online (Sandbox Code Playgroud)
由于Firestore的价格,该解决方案显然不是一个选择。此外,如果文档数量很多,执行请求可能会很长。
2.使用Elasticsearch或Algolia
这可能很有趣。但是,我认为将这些解决方案的支持仅用于模式搜索会有些过头,而且这很快就会变得昂贵。
3. searchName创建对象时的自定义字段
因此,我有了以下解决方案:在创建文档时,创建一个包含一系列可能的搜索模式的字段:
{
...
"name":"adidas",
"searchNames":[
"adi",
"adida",
"adidas"
],
...
}
Run Code Online (Sandbox Code Playgroud)
这样就可以使用来访问文档:
filteredBrands = db.collection("brands").where("searchNames", "array-contains", "pattern");
Run Code Online (Sandbox Code Playgroud)
所以我有几个问题:
我的模板中有这样的东西:
<span *ngIf="selectedSport.key === 'walking'"> steps </span>
<span *ngIf="selectedSport.key !== 'walking'"> km </span>
Run Code Online (Sandbox Code Playgroud)
我觉得这个拼写很难看,而且这两条线就是这个......呃.所以我试着寻找替代方案.
NgIfElse
<span *ngIf="selectedSport.key === 'walking'; else elseSpan"> steps </span>
<ng-template #elseSpan> km </ng-template>
Run Code Online (Sandbox Code Playgroud)
我发现这个更好,但是在多条件的情况下使用它可能很棘手*ngIf="A && B".我们在模板中仍然有两个代码行...
获得功能
<span> {{getUnit(selectedSport.key)}} </span>
Run Code Online (Sandbox Code Playgroud)
getUnit(sportKey: string): string {
return sportKey === 'walking' ? 'steps' : 'km';
}
Run Code Online (Sandbox Code Playgroud)
随着模板的可读性越来越高,这样做会更好.但是我不希望在我的组件中添加一个函数.
你知道Angular 2+模板是否支持getUnit函数中的三元运算符?
你有更好的主意吗?
我开始为 FCM 实现后台消息,他们要求我制作自己的 Kotlin 应用程序。但自从我这样做以来,我的调用堆栈中似乎有 2 个主要函数。
这怎么可能?如果我运行一个空项目,它的调用堆栈中有 0 个电源吗?
我添加了Application.kt
package HERE_I_HAVE_MY_PACKAGE_NAME
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
public class Application: FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
}
override fun registerWith(registry: PluginRegistry) {
FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
}
}
Run Code Online (Sandbox Code Playgroud)
和 FirebaseCloudMessagingPluginRegistrant.kt
package HERE_I_HAVE_MY_PACKAGE_NAME
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
class FirebaseCloudMessagingPluginRegistrant {
companion object {
fun registerWith(registry: PluginRegistry) {
if (alreadyRegisteredWith(registry)) {
return;
}
FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
}
fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
val key = FirebaseCloudMessagingPluginRegistrant::class.java.name
if (registry.hasPlugin(key)) {
return …Run Code Online (Sandbox Code Playgroud) 我想在我的页面上有一个可滚动的 div 区域,并将我的 div CSS 上的溢出设置为可滚动,但现在我想在所有浏览器中隐藏滚动条,因此 div 可以在没有滚动条/面板显示的情况下滚动,包括隐藏它Firefox 中糟糕的滚动面板,我知道如何在 chrome 中隐藏滚动条,但这在 Firefox 中不起作用,有人能提出什么建议吗,谢谢?
这是我用来在 chrome 和其他 webkit 浏览器中隐藏滚动条的方法:
#element::-webkit-scrollbar {
display: none;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个可观察的列表(即当列表的元素被更改、删除或添加时,我希望 UI 更新)。我知道 mobx 有一个叫做“observableList”的东西,所以这看起来应该是可能的。但是,我在实施它时遇到了问题。我目前在我的 mobx 商店文件中有以下代码:
var metrics = Observable([.5,.5,.5]);
Run Code Online (Sandbox Code Playgroud)
然后,我尝试更改其中一个元素,如下所示:
metrics[index] = data;
Run Code Online (Sandbox Code Playgroud)
我收到错误:
方法 '[]=' 没有为类 'Observable>' 定义。
有没有办法在颤振中创建一个可观察的列表(或者更好的是,一个可观察的字典),或者还没有实现?
谢谢!
当我粘贴my_example_link到浏览器中时,文件会以适当的方式自动下载。但是,当我使用下面显示的源代码时,下载不起作用。单击download按钮后,我想下载文件。任何想法有什么问题吗?我没有任何错误。
user.service.ts:
DownloadFiles() {
return this.http.get('my_example_link', {responseType: 'text'});
}
Run Code Online (Sandbox Code Playgroud)
uploader.service.ts:
DownloadFile(){
this.userService.DownloadFiles()
.subscribe(
(data) => this.downloadFile2(data)), // console.log(data),
(error) => console.log("Error downloading the file."),
() => console.info("OK");
}
downloadFile2(data: Response){
var blob = new Blob([data], { type: 'text/csv' });
var url= window.URL.createObjectURL(blob);
window.open(url);
}
Run Code Online (Sandbox Code Playgroud)
something.component.html:
<li class="nav-item" *ngIf="isCloud()">
<a (click)="this.UploaderService.DownloadFile()" download="file23.txt" style='cursor: pointer;' class="nav-link" target="_blank">
<i class="nc-icon nc-basket"></i> Download
</a>
</li>
Run Code Online (Sandbox Code Playgroud) 我有一个服务:
import { EventEmitter, Injectable } from '@angular/core';
@Injectable()
export class PingOnActionListenerService {
removingListener = new EventEmitter();
removeListener(): void {
this.removingListener.emit();
}
Run Code Online (Sandbox Code Playgroud)
在组件 AI 处调用服务:
this._pingOnActionListenerService.removeListener();
Run Code Online (Sandbox Code Playgroud)
在组件BI想要监听服务:
ngOnInit() {
this._pingOnActionListenerService.removingListener.subscribe(this.deactivatelistener());
}
deactivatelistener() {
window.removeEventListener('click', this.pingIfLastPingIsOld);
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我在控制台上收到错误消息:
core.js:1671 错误 TypeError:generatorOrNext 不是 SafeSubscriber.schedulerFn [as _next] (core.js:3565) 的函数,位于 SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:195) 在 SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:133) 在 Subscriber.push../node_modules/rxjs/_esm5/internal /Subscriber.js.Subscriber._next (Subscriber.js:77) 在 Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54) 在 EventEmitter.push.. /node_modules/rxjs/_esm5/internal/Subject.js.Subject.next (Subject.js:47) 在 EventEmitter.push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit (core.js :3537) 在PingOnActionListenerService.push../src/app/core/services/ping-on-action/ping-on-action-listener.service.ts.PingOnActionListenerService.removeListener (ping-on-action-listener.service. ts:19) 在 CatchSubscriber.selector (logging-interceptor.ts:45) 在 CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:33)