Flutter Hard Code in Web App

Flutter hard code is just like any other hard code principle that we follow when we develop any flutter app.

Hard code is something that we embed into our code. Neither it comes from any external source, nor we change the value on runtime. Right? Flutter hard code is no exception.

For example, we’ve seen the same instance in our Firebase, Firestore and Provider web app.

What do we see on the screen?

Let’s first see the home page of our flutter web app.

User can select a portion of text with selectable text in flutter
User can select a portion of text with selectable text in flutter

In the lower half of the screen there are two blog posts that we have hard coded in our blog class.

As a result, when we click the link, the blog page opens up.

Flutter web app blog page
Flutter web app blog page

The title, date and the body are instances of flutter hard code.

Let’s see the same post in our code.

import 'package:intl/intl.dart';

class BlogPost {
  final String title;
  final DateTime publishedDate;
  final String body;

  String get date => DateFormat('d MMMM y').format(publishedDate);

  BlogPost({
    required this.title,
    required this.publishedDate,
    required this.body,
  });
}

final blogPosts = [
  BlogPost(
    title: 'What is Peace?',
    publishedDate: DateTime(2022, 5, 2),
    body: 'A wrapper around our mental state that gives us stability.',
  ),
  BlogPost(
    title: 'How to get Peace?',
    publishedDate: DateTime(2022, 4, 3),
    body: 'We need to avoid greed and be content with what we\'ve got.',
  ),
];

We’re not elaborating further. Because in our previous section we have seen how we have used a provider package to send and receive the data.

However, the question remains. 

Is flutter hard code good?

Whether the instances of such “flutter hard code” is good or bad?

Certainly, as we build the app, the act of resorting to such flutter hard code is not bad. Because we’re in a development mode.

But when we go to the production mode, this data will come from an external source. 

In our case, the data will come from the Firestore database.

Therefore, as we progress we’ll see how we can replace such flutter hard code with dynamic data.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Technical blog

Twitter


by

Comments

2 responses to “Flutter Hard Code in Web App”

  1. […] we were discussing flutter 3.0 web app, we had hard coded the initial blog data. There was no flutter sign in method so that the user […]

  2. […] as we were discussing flutter 3.0 web app, initially it was […]

Leave a Reply