Omar Alzarea
Omar Alzarea

Reputation: 21

How Can I Put The Current Date In Text Widget?

Hello everyone I'm beginner in flutter..

I'm building an Alarm app using dart and flutter but when I try to put the current date in the text widget I have an error

A non-null String must be provided to a Text widget.

Failed assertion: line 370 pos 10: 'data != null'

and this my code

class _MyHomePageState extends State<MyHomePage> {


String greetingMessage() {
var timeNow = DateTime.now().hour;

if (timeNow <= 11.59) {
  return 'Good Morning';
} else if (timeNow > 12 && timeNow <= 16) {
  return 'Good Afternoon';
} else if (timeNow > 16 && timeNow < 20) {
  return 'Good Evening';
} else {
  return 'Good Night';
}

String finalDate = '';



getCurrentDate() {
var date = DateTime.now().toString();

var dateParse = DateTime.parse(date);

var formattedDate = "${dateParse.day}-${dateParse.month}-${dateParse.year}";

setState(() {
  finalDate = formattedDate.toString();
});



Padding(
                padding: const EdgeInsets.only(top: 30, bottom: 18),
                child: Text(
                  greetingMessage(),
                  style:
                      TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                ),
              ),
              Text(
                getCurrentDate(),
                style: TextStyle(fontSize: 18, color: Colors.black54),

if anyone can help me I appreciate your effort thanks.

Upvotes: 1

Views: 5643

Answers (2)

Mukul
Mukul

Reputation: 1155

To Show the Date in Text widget, You Need to return the Date from the getCurrentDate() method, While Accessing your getCurrentDate() method in Text Widget otherwise it will return a null and Show an error.

In Simple Words : Just add a return statement to your method and return your formattedDate.

getCurrentDate() {
   var date = DateTime.now().toString();

   var dateParse = DateTime.parse(date);

   var formattedDate = "${dateParse.day}-${dateParse.month}-${dateParse.year}";

   return formattedDate;
}

Upvotes: 0

Pratik Butani
Pratik Butani

Reputation: 62419

Your code is not formatted and You had written Padding and TextField Widget without having Column or that type of widget which can display code in vertically.

Here is full working code:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 30, bottom: 18),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            greetingMessage(),
            style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
          ),
          Text(getCurrentDate(),
              style: TextStyle(fontSize: 18, color: Colors.cyan))
        ],
      ),
    );
  }

  String greetingMessage() {
    var timeNow = DateTime.now().hour;

    if (timeNow <= 11.59) {
      return 'Good Morning';
    } else if (timeNow > 12 && timeNow <= 16) {
      return 'Good Afternoon';
    } else if (timeNow > 16 && timeNow < 20) {
      return 'Good Evening';
    } else {
      return 'Good Night';
    }
  }

  String getCurrentDate() {
    var date = DateTime.now().toString();

    var dateParse = DateTime.parse(date);

    var formattedDate = "${dateParse.day}-${dateParse.month}-${dateParse.year}";
    return formattedDate.toString();
  }
}

Output:

enter image description here

Thank you.

Upvotes: 1

Related Questions