Home > database >  How to Remove Text null value and Enter a Specific Value in all Container in Flutter
How to Remove Text null value and Enter a Specific Value in all Container in Flutter

Time:03-15

import 'package:flutter/material.dart';

class AssignClone extends StatelessWidget {
  const AssignClone({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        backgroundColor: Colors.teal,
        centerTitle: true,
        title: Text('First Page'),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(6.0),
          child: Column(
            children: [
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
            ],
          ),
        ),
      ),
    );
  }

  Expanded clone_2(Color c, [Text? t]) {
    return Expanded(
      child: Container(
        child: Text('$t'),     // in this line it's show null please fix it. 
        width: double.infinity,
        height: 70,
        decoration: BoxDecoration(
          color: c,
        ),
      ),
    );
  }
}

CodePudding user response:

Pass values for t like so:

class AssignClone extends StatelessWidget {
  const AssignClone({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        backgroundColor: Colors.teal,
        centerTitle: true,
        title: Text('First Page'),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(6.0),
          child: Column(
            children: [
              clone_2(Colors.amber, "amber"),
              clone_2(Colors.pink, "pink"),
              clone_2(Colors.blue, "blue),
            ],
          ),
        ),
      ),
    );
  }

  Expanded clone_2(Color c, [Text? t]) {
    return Expanded(
      child: Container(
        child: Text('$t'),     // in this line it's show null please fix it. 
        width: double.infinity,
        height: 70,
        decoration: BoxDecoration(
          color: c,
        ),
      ),
    );
  }
}

CodePudding user response:

import 'package:flutter/material.dart';
class AssignClone extends StatelessWidget {
  const AssignClone({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        backgroundColor: Colors.teal,
        centerTitle: true,
        title: Text('First Page'),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(6.0),
          child: Column(
            children: [
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
              clone_2(Colors.amber),
              clone_2(Colors.pink),
              clone_2(Colors.blue),
            ],
          ),
        ),
      ),
    );
  }

  Expanded clone_2(Color c, [Text? t]) {
    return Expanded(
      child: Container(
       // child: Text('$t'),     // in this line it's show null please fix it. 
       child: Text(t().data != null ?? '$t().data' : 'Your default text here'),
        width: double.infinity,
        height: 70,
        decoration: BoxDecoration(
          color: c,
        ),
      ),
    );
  }
}
  • Related