Month: July 2022

  • API testing in Flutter

    API testing in Flutter

    It is very common to use API in our Flutter projects. Testing makes the Flutter project very easy to maintain, in this way you will save a lot of debugging time. Also when you have testing in place you can always check whether API working as expected. 

    Let’s create a simple API endpoint in PHP in our local server. If you don’t anything about PHP web development, It’s quite fun to work in PHP. This PHP file just responds with a JSON which has a list of Books, nothing complex right? 

    Server:

    <?php
    
    $b1 = ['title' => 'Beginning App Development with Flutter', 'author' => 'Rap Payne'];
    $b2 = ['title' => 'Beginning Flutter: A Hands On Guide to App Development', 'author' => 'Marco L. Napoli'];
    $b2 = ['title' => 'Flutter Apprentice', 'author' => 'Vincenzo Guzzi, Kevin D Moore, Vincent Ngo and Michael Katz'];
    
    $books = [$b1, $b2];
    
    header("content-type: application/json; charset=UTF-8");
    echo json_encode($books, JSON_PRETTY_PRINT);
    
    [
        {
            "title": "Beginning App Development with Flutter",
            "author": "Rap Payne"
        },
        {
            "title": "Flutter Apprentice",
            "author": "Vincenzo Guzzi, Kevin D Moore, Vincent Ngo and Michael Katz"
        }
    ]

    Flutter:


    Let’s first create a flutter project called “api_test”, we will use VSCode for the rest of the project.

    flutter create api_test

    Let’s add http package by this command, run on your terminal.

    flutter pub add http

    Go to your lib folder and create two files model.dart and service.dart.

    Model: 

    On the model.dart creates a book class.

    class Book {
      final String title;
      final String author;
    
      Book({required this.title, required this.author});
    }

    API:

    In the service, class create an abstract class like this 

    abstract class BookApi {
    import 'package:api_test/model.dart';
    import 'package:http/http.dart' as http;
    import 'dart:convert' as convert;
    
    abstract class BookApi {
      Future<List<Book>> books();
    }
    
    class BookService implements BookApi {
      Future<List<Book>> books() async {
        var url = Uri.parse('http://localhost/flutter/book.php');
        var response = await http.get(url);
    
        //print(response.body);
        if (response.statusCode == 200) {
          final listResponse = convert.jsonDecode(response.body) as List;
          List<Book> books = listResponse
              .map(
                (e) => Book(
                  title: e['title'],
                  author: e['author'],
                ),
              )
              .toList();
          return books;
        } else {
          print('Request failed with status: ${response.statusCode}.');
          return [];
        }
      }
    }
    
    

    create another class in the same file for the above BookApi method implementation

    Test:

    Create a test file called book_service_test.dart in test folder

    import 'package:api_test/model.dart';
    import 'package:api_test/service.dart';
    import 'package:flutter_test/flutter_test.dart';
    
    void main() {
      var book_service = BookService();
    
      test('book api', () async {
        final result = await book_service.books();
        print(" test $result");
    
        expect(result.isNotEmpty, true);
        expect(result, isA<List<Book>>());
      });
    }
    

    Our tests are passed when the list is not empty and list has a book type.

    Download the full source code

    Spread the love
  • SwiftUI for Flutter developer – Video 1

    As a flutter developer, there are not many things you can do on Xcode since Flutter itself builds everything for you but why not learn some app development with Swift and Xcode.

    Swift is a deep and large language and I find it complex, when I learned Objective-C back in 2010 with Xcode 3.x that was very hard to start but later it became easy for me.

    Today there is no reason to start learning Objective-C over Swift unless you have a specific interest.

    For Flutter developers, if flutter is your first mobile framework and dart is the first language you may find Swift is complex and hard to start but the idea behind all the languages is the same you just have to learn the syntax. Of course, some languages have specific features for some reasons.

    I have a series of videos for Flutter developers who want to learn Swift App Development with Xcode. For running Xcode you just need a Mac computer. Running Xcode on any Mac will work, most of the time you don’t need a very powerful Mac.

    I’m very new to making videos and teaching programming to others, if you find anything wrong, just let me know in the comments. Also if you can subscribe to my channel and like the video, I’m gonna appreciate that.

    So this is the first video, there going to be a few more videos. We are going to make an app with a login page and list page. We also going to use some APIs which also we are going to make ourselves in PHP.

    https://www.youtube.com/watch?v=f4kXQBsZBoI
    Spread the love