2

我正在尝试实现添加电子邮件输入列表但用户的功能。这就是我到目前为止所拥有的

  List<String> _notificationEmails =[];
  var _notificationEmailsController = TextEditingController();
  Widget _buildNotificationEmailsInput() {
    return TextFormField(
      controller: _notificationEmailsController,
      style: inputTextStyle,
      maxLines: null,
      validator: (String value) {
        print(value);
        if (value.isEmpty) {
          return 'Emails Required';
        }
        return null;
      },
      onChanged: (String value){
        if(value.substring(value.length-1)==','){
          print('here');
            setState(() {
              _notificationEmails.add(value.substring(0,value.length-1));
            });
          _notificationEmailsController.clear();
        }
        print(_notificationEmails);
      },
    );
  }

我的预期结果是,当用户输入一封电子邮件然后在其后添加一个逗号时,该电子邮件被附加到列表中并且输入字段被清除,但我得到一个导致此 _notificationEmails.add(value.substring(0,value.length-1)); 运行到无穷大的操作循环。

这是日志

[   +3 ms] flutter: [je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gmail.com, je@gm<…&gt;

这种情况一直在继续。我做错了什么?

更新

如果它有帮助,我已经意识到这个问题是因为每次_notificationEmailsController.clear();被调用时,它都会触发onChange()循环。

4

2 回答 2

2

更新:请TextEditingController.clearFuture.delayed. 因为根据clear...的描述

此方法只应在帧之间调用,例如响应用户操作,而不是在构建、布局或绘制阶段。

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyApp createState() => _MyApp();
}

class _MyApp extends State<MyApp> {
  final List<String> _notificationEmails = <String>[];
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            _buildNotificationEmailsInput(),
            Expanded(
              child: ListView.builder(
                itemCount: _notificationEmails.length,
                itemBuilder: (_, int idx) => ListTile(
                  title: Text(_notificationEmails[idx]),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildNotificationEmailsInput() {
    return TextFormField(
      controller: _controller,
      validator: (String value) {
        print('VALIDATOR: $value');
        if (value.isEmpty) {
          return 'Emails Required';
        }
        return null;
      },
      onChanged: (String value) {
        if (value.substring(value.length - 1) == ',') {
          print('>>>>>> value = $value : controller = ${_controller.hashCode}');
          setState(() {
            _notificationEmails.add(value.substring(0, value.length - 1));
          });
          Future<void>.delayed(
            const Duration(milliseconds: 10),
            _controller.clear,
          );
          print(_notificationEmails);
        }
      },
    );
  }
}
于 2021-02-05T13:33:51.003 回答
1
**You have to add a list of *TextEditingController* and need to add the contoller text to that list and parse it as you need.**     
     
      List<String> selection = [];  
     List<Product> productList = []; 

     //---------Adding contoller to list   
      
     productProvider.getAll(user.guid).forEach((element) {//---List<Product>
     final TextEditingController quantityController = 
     TextEditingController(text: element.quantity);
     quantityControllers.add(quantityController);
     });

       //-------Adding list of products to list
      List<Map<String, dynamic>> productItems = [];
       List<Product> productOriginalList = 
       productProvider.getAll(user.guid);
       for (int i = 0; i < productOriginalList.length; i++) {
       final Product product = productOriginalList[i];
       if (selection.contains(product.equipmentId)) {
                   
       productItems.add(product.toJson(quantityControllers[i].text));
                  }

     /* Map<String, dynamic> toJson(String quan) => {
    'ProductId': id,
    'Quantity': quan,
     };     
    TextField(
    controller: quantityControllers[index],*/
     
于 2021-10-28T05:03:08.210 回答