如何从json获取数据中对两个或多个值求和

Posted

技术标签:

【中文标题】如何从json获取数据中对两个或多个值求和【英文标题】:How to sum two or more values from getting data from json 【发布时间】:2021-03-01 12:07:45 【问题描述】:

我有一个用于在颤振中创建列表视图的 json 文件。单击列表项时,它会显示从 json 获取的值。但我想在点击事件上添加新的未来。我想通过加在一起显示的两个值来显示新值。我该怎么做?

这是我关于列表视图的点击事件的示例

('Formül: ' + _searchResult[i].Formula + '\nTfp: ' + _searchResult[i].Tfp.toString() +'\nTb: ' + _searchResult[i].Tb.toString() +'\nTc: ' + _searchResult[i].Tc.toString() +'\nPc: ' + _searchResult[i].Pc.toString() +'\nVc: ' + _searchResult[i].Vc.toString() +'\nZc: ' + _searchResult[i].Zc.toString() +'\nOmega: ' + _searchResult[i].Omega.toString() +'\nDipm: ' + _searchResult[i].Dipm.toString() +'\nCpA: ' + _searchResult[i].CpA.toString()+'\nCpB: ' + _searchResult[i].CpB.toString() +'\nCpC: ' + _searchResult[i].CpC.toString() +'\nCpD: ' + _searchResult[i].CpD.toString() +'\ndHf: ' + _searchResult[i].dHf.toString() +'\ndGf: ' + _searchResult[i].dGf.toString() +'\nEq: ' + _searchResult[i].Eq.toString()+'\nLden: ' + _searchResult[i].Lden.toString() +'\nTden: ' + _searchResult[i].Tden.toString()

例如,我想在此处添加名为 newvalue 的新值,它等于 cpA 和 cpB 值的总和。 谢谢。

编辑:这是我的颤振代码

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'userDetails.dart';

class HomePage extends StatefulWidget 
  HomePage(Key key) : super(key: key);

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


class _HomePageState extends State<HomePage> 
  List<UserDetails> _searchResult = [];
  List<UserDetails> _userDetails = [];
  final customCp = 0;
  TextEditingController controller = new TextEditingController();




  // Get json result and convert it to model. Then add
  Future<Null> getUserDetails() async 
    final response = await http.get(url);
    final responseJson = json.decode(response.body);

    setState(() 
      for (Map user in responseJson) 
        _userDetails.add(UserDetails.fromJson(user));
      
    );
  

  @override
  void initState() 
    super.initState();

    getUserDetails();
  

  Widget _buildUsersList() 

    return new ListView.builder(

      itemCount: _userDetails.length,
      itemBuilder: (context, index) 
        return new Card(
          child: ListTile(
            leading: FlutterLogo(size: 56.0),
            title: Text(_userDetails[index].Name),
            subtitle: Text(_userDetails[index].Formula),


          onTap: ()
            AlertDialog alert = AlertDialog(

              title: Text('' + _userDetails[index].Name.toUpperCase()),
              content: Text('Formül: ' + _userDetails[index].Formula + '\nTfp: ' + _userDetails[index].Tfp.toString() +'\nTb: ' + _userDetails[index].Tb.toString() +'\nTc: ' + _userDetails[index].Tc.toString() +'\nPc: ' + _userDetails[index].Pc.toString() +'\nVc: ' + _userDetails[index].Vc.toString() +'\nZc: ' + _userDetails[index].Zc.toString() +'\nΩ: ' + _userDetails[index].Omega.toString() +'\nDipm: ' + _userDetails[index].Dipm.toString() +'\nCpA: ' + _userDetails[index].CpA.toString()+'\nCpB: ' + _userDetails[index].CpB.toString() +'\nCpC: ' + _userDetails[index].CpC.toString() +'\nCpD: ' + _userDetails[index].CpD.toString() +'\ndHf: ' + _userDetails[index].dHf.toString() +'\ndGf: ' + _userDetails[index].dGf.toString() +'\nEq: ' + _userDetails[index].Eq.toString() +'\nLden: ' + _userDetails[index].Lden.toString() +'\nTden: ' + _userDetails[index].Tden.toString()),
              actions: [
              ],
            );
            showDialog(
              context: context,
              builder: (BuildContext context) 
                return alert;
              ,
            );

          ,
          ),
        );

      ,
    );
  

  Widget _buildSearchResults() 
    return new ListView.builder(
      itemCount: _searchResult.length,
      itemBuilder: (context, i) 
        return new Card(
          child: new ListTile(
            leading: FlutterLogo(size: 56.0),

            title: new Text(
                _searchResult[i].Name),
            subtitle: Text(_searchResult[i].Formula),
            onTap: ()
              AlertDialog alert = AlertDialog(
                title: Text(_searchResult[i].Name.toUpperCase()),
                content: Text('Formül: ' + _searchResult[i].Formula + '\nTfp: ' + _searchResult[i].Tfp.toString() +'\nTb: ' + _searchResult[i].Tb.toString() +'\nTc: ' + _searchResult[i].Tc.toString() +'\nPc: ' + _searchResult[i].Pc.toString() +'\nVc: ' + _searchResult[i].Vc.toString() +'\nZc: ' + _searchResult[i].Zc.toString() +'\nOmega: ' + _searchResult[i].Omega.toString() +'\nDipm: ' + _searchResult[i].Dipm.toString() +'\nCpA: ' + _searchResult[i].CpA.toString()+'\nCpB: ' + _searchResult[i].CpB.toString() +'\nCpC: ' + _searchResult[i].CpC.toString() +'\nCpD: ' + _searchResult[i].CpD.toString() +'\ndHf: ' + _searchResult[i].dHf.toString() +'\ndGf: ' + _searchResult[i].dGf.toString() +'\nEq: ' + _searchResult[i].Eq.toString()+'\nLden: ' + _searchResult[i].Lden.toString() +'\nTden: ' + _searchResult[i].Tden.toString()),actions: [
                ],
              );
              showDialog(
                context: context,
                builder: (BuildContext context) 
                  return alert;
                ,
              );
            ,

          ),

        );
      ,
    );
  

  Widget _buildSearchBox() 
    return new Padding(
      padding: const EdgeInsets.all(8.0),
      child: new Card(
        child: new ListTile(
          leading: new Icon(Icons.search),
          title: new TextField(
            controller: controller,
            decoration: new InputDecoration(
                hintText: 'Ara', border: InputBorder.none),
            onChanged: onSearchTextChanged,
          ),
          trailing: new IconButton(
            icon: new Icon(Icons.cancel),
            onPressed: () 
              controller.clear();
              onSearchTextChanged('');
            ,
          ),
        ),
      ),
    );
  

  Widget _buildBody() 
    return new Column(
      children: <Widget>[
        new Container(
            color: Theme.of(context).primaryColor, child: _buildSearchBox()),
        new Expanded(
            child: _searchResult.length != 0 || controller.text.isNotEmpty
                ? _buildSearchResults()
                : _buildUsersList()),
      ],
    );
  

  @override
  Widget build(BuildContext context) 
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Equation Of State'),
        elevation: 0.0,
      ),
      body: _buildBody(),
      resizeToAvoidBottomPadding: true,
    );
  

  onSearchTextChanged(String text) async 
    _searchResult.clear();
    if (text.isEmpty) 
      setState(() );
      return;
    

    _userDetails.forEach((userDetail) 
      if (userDetail.Name.toLowerCase().contains(text.toLowerCase()) ||
          userDetail.Formula.toLowerCase().contains(text.toLowerCase())) _searchResult.add(userDetail);
    );

    setState(() );
  

【问题讨论】:

请添加您页面的一些代码。 您需要用两个不同的按钮将这两个功能分开。如果您想向该 JSON 添加一个值,则应由不同的按钮提供。 我在linked.in上给你写信:) 【参考方案1】:

您可以在没有任何库的情况下使用这四种基本操作。如果需要更多,可以导入math library。

以下是求和运算示例:

    Text(
      "Sum of CpA and CpB is: " +
          (_userDetails[index].CpA + _userDetails[index].CpB).toString(),
    ),

【讨论】:

【参考方案2】:

//在模型类中尝试

class User-Details
  final String name;
  final String formula;
  final String name&Formula;
  User-Details(this.name, this.formula, this.name&Formula);

;

【讨论】:

然后呢?你明白问题了吗? 您想从 JSON 数据中收集两个值并在点击 listview 时将它们显示在 New Flutter 中? 没有。我不想收集。只有我显示从 json 获得的两个或多个值的总和。例如 value1 = 10 , value2 = 20 我想向用户显示 10+20=30

以上是关于如何从json获取数据中对两个或多个值求和的主要内容,如果未能解决你的问题,请参考以下文章

当我们压缩到视图时,如何对从控制器获取的 JSON 数组中的数据求和(Laravel)

数据库:如何从具有一个公共列的两个表中获取多个值

如何使用 JS 获取不同的值并在 JSON 上求和

如何在 Python 中对每列的唯一值求和? [复制]

我如何在 Xamarin 中读取两个同名的 json 数据

如何使用会话ID从php mysql中的两个或多个表中获取值