Home > Net >  Write a Dart program to concate list element value with hello
Write a Dart program to concate list element value with hello

Time:04-27

For exmple, input: [1, 2, 3] output: [1 hello, 2 hello, 3 hello]

CodePudding user response:

You can try like this.

void main() {
 List a = [1, 2, 3];
    List b = [];
    for (var element in a) {
      b.add("$element hello");
    }

    print(b);
}

CodePudding user response:

for(var i = 0; i < list.length; i  ){
   list.elementAt(i) = list.elementAt(i).toString()   "hello";
}

CodePudding user response:

import 'package:flutter/material.dart';
List<String> numbers = ['1','2','3'];
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        itemCount:  numbers.length,
        itemBuilder: (BuildContext context, int index){
        return Center(child: Text('${numbers[index]} hello'));
      }))
    ;
  }
}

CodePudding user response:


void main(){
  
  List<int> input=[1,2,3];
  List<dynamic> output=[];
  input.forEach((element){
    output.add((element.toString() " hello"));
  });
  
  print(output);//[1 hello, 2 hello, 3 hello]
}
  • Related