小编DxW*_*DxW的帖子

如何在Dart中去掉Textfield onChange?

我正在尝试开发一个TextField,在更改时更新Firestore数据库上的数据.它似乎工作,但我需要防止onChange事件多次触发.

在JS我会使用lodash _debounce()但在Dart中我不知道该怎么做.我已经阅读了一些去抖库但我无法弄清楚它们是如何工作的.

这是我的代码,它只是一个测试,所以有些奇怪的东西:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


class ClientePage extends StatefulWidget {

  String idCliente;


  ClientePage(this.idCliente);

  @override
  _ClientePageState createState() => new _ClientePageState();


}

class _ClientePageState extends State<ClientePage> {

  TextEditingController nomeTextController = new TextEditingController();


  void initState() {
    super.initState();

    // Start listening to changes 
    nomeTextController.addListener(((){
        _updateNomeCliente(); // <- Prevent this function from run multiple times
    }));
  }


  _updateNomeCliente = (){

    print("Aggiorno nome cliente");
    Firestore.instance.collection('clienti').document(widget.idCliente).setData( {
      "nome" : nomeTextController.text
    }, merge: true);

  }



  @override
  Widget build(BuildContext context) {

    return new StreamBuilder<DocumentSnapshot>(
      stream: Firestore.instance.collection('clienti').document(widget.idCliente).snapshots(), …
Run Code Online (Sandbox Code Playgroud)

dart debouncing flutter

19
推荐指数
8
解决办法
5039
查看次数

在领域侦听器回调中调用setState时,直到点击屏幕,UI才会更新

更新

描述

我在Realm对象上有一个侦听器,用于获取更新。当服务器(或客户端)上有更新时,提供给侦听器的函数将调用setState({})。

奇怪的是,即使控制台说一切正常,并且表明render方法是用正确的数据调用的,我也看不到我的应用程序有任何更新。

如果我随机地(在1s,2s,20s ...之后)点击屏幕,UI会神奇地更新,并且一切正确。

如果我使用从按钮调用的函数执行相同的setState,那么我猜是因为按钮的动画触发了UI更新。

感谢您阅读本文。

制作步骤

您必须更新server_url和凭据才能工作。

react-native初始化测试npm install realm react-native链接域

由于领域尚未为64位做好准备,因此您还必须确保仅在32位中进行编译,以避免启动时应用崩溃

使用此代码:

App.js

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';

import Realm from 'realm'

import { SERVER_URL } from "./config/realm";
import { Utente } from "./config/schema";


export default class App extends Component {

  loginAsync = async () => {

    var realm_user = Realm.Sync.User.current

    if(!realm_user){
      const credentials = Realm.Sync.Credentials.usernamePassword('admin', '******' ,false);
      realm_user = await Realm.Sync.User.login(SERVER_URL, credentials);
    }

    const config = realm_user.createConfiguration({
      schema: …
Run Code Online (Sandbox Code Playgroud)

android realm reactjs react-native realm-js

8
推荐指数
2
解决办法
487
查看次数

javascript中的"属性描述必须是对象"错误.无法理解为什么:(

使用此代码我有这个问题:

    $.fn.dxwShow = function (options) {

    console.log(typeof(options));
    dxwShowSetOptions(options);

    setInterval(function(){
        dxwShowChange();
    }, dxwShowOptions.time);
};

var dxwShowOptions = {
    "transition" : "SlideToggle",
    "time": 1000
};

var dxwShowStatus = {
    current : 0
};

function dxwShowSetOptions(options){
    console.dir(typeof(options));

    dxwShowOptions = Object.create(dxwShowOptions, options);
}

function dxwShowChange(){
    console.log(dxwShowOptions);
};

$(function(){

    options = {
        "time": 700,
        "debug" : true
    };

    $("#dxwShow").dxwShow(options);

});
Run Code Online (Sandbox Code Playgroud)

我想更新dxwShowOptions,所以我使用Object.create首先传递我想要复制的对象,因此包含新参数的对象.哪里出错了?

PS:Chrome说该对象位于Object.create行.

oop jquery

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

dart ×1

debouncing ×1

flutter ×1

jquery ×1

oop ×1

react-native ×1

reactjs ×1

realm ×1

realm-js ×1