Code Optimization

Interesting things about software development and code optimization

The Kotlin Language Client server crashed 5 times in the last 3 minutes. The server will not be restarted.

Hello friends,


seems like you have faced the same issue like me ;)


What I can say is that I was not able to fully solve this issue but I solved it partially at least.

So if you see this error in the Visual Studio Code:

The Kotlin Language Client server crashed 5 times in the last 3 minutes. The server will not be restarted.

then you have Kotlin extension installed and while updating it you hit this issue.


To be able to work, build and debug my app i did the following:

- uninstall the Kotlin extension


yes - not just disable but uninstall it and now you can continue working with Flutter & Dart


please note, even you have Kotlin Language extension that is more similar to the error the real problem is in the Kotlin extension itself.


If I will find out further steps to resolve it completely I will update this post.


Thank you and see you soon! :)



1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Flutter - Updating TextField Selection/Caret Problem and comma for thousand separator

Hello friends,


I'm going to show you how I resolved the selection/caret updating issue for Flutter TextField when cursor goes to the very beginning instead of the position you specify.


I was creating a money formating text field and faced this issue as many of us and here is how I resolved it:

  
String odlValue="";

  @override
  void initState() {
    tbPurchasePrice.addListener((){
      var cursor = tbPurchasePrice.selection;
      String val = tbPurchasePrice.text;
      if((odlValue.length < val.length && val.length > 3) || 
          (odlValue.length > val.length && val.length > 4)){
        val = val.replaceAll(",""");
        val = val.substring(0, val.length - 3) + "," + val.substring(val.length - 3);
        int d = val.length - tbPurchasePrice.text.length;
        odlValue = val;
        tbPurchasePrice.text = val;
        Future.delayed(
        Duration(milliseconds: 5),
         () {
              tbPurchasePrice.selection = new TextSelection(
baseOffset: cursor.baseOffset + d, 
               extentOffset: cursor.extentOffset + d, 
               affinity: cursor.affinity, isDirectional: cursor.isDirectional);  
          });
      }
      else if(odlValue.length > val.length && val.length <= 4 && val.indexOf(",") >= 0){
        tbPurchasePrice.text = val.replaceAll(",""");
        odlValue = val;
        Future.delayed(
        Duration(milliseconds: 5),
         () {
              tbPurchasePrice.selection = new TextSelection(
baseOffset: cursor.baseOffset - 1
               extentOffset: cursor.extentOffset - 1
               affinity: cursor.affinity, isDirectional: cursor.isDirectional);  
          });
      }
      odlValue = val;
    });

    super.initState();
  }

.......

child: TextFormField(
       decoration: InputDecoration( prefix: Text(r"$")),
       controller: tbPurchasePrice, 
keyboardType: TextInputType.numberWithOptions(decimal: true),
       style: Theme.of(context).textTheme.headline,
   )
.......


so it puts $ sign on the beginning, then it puts comma in the first thousand place and it use delayed function call to set/update the selection property so it would not go to the beginning of the textfield but to the same place where it has been before text updating or where you need it to be.



Thank you and see you ;)




1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Flutter - SingleChildScrollView with continuous scrolling pages

Hi friends,


I was recently developing some mobile app in flutter and was requested to create auto-paging on scrolling.

So when user scrolls to the bottom it would go to/append the next page of content and this is inside of the simple 

SingleChildScrollView (of course as I was already using the SingleChildScrollView as main container :) ).


We have the scroll controller property but what it gives us? Offset and scroll direction in general but how we will use it to make paging?

We need to detect the bottom of the current page list and we should allow user to interact with last item in the current list before going to or appending a new page.


So here is my solution for it:


  ScrollController _controller;
  int bottomOutOfRange = 0;

  _scrollListener() {
    if (_controller.offset > 0.0 &&
        _controller.position.maxScrollExtent > 0.0) {
      if (_controller.offset >= _controller.position.maxScrollExtent &&
          !_controller.position.outOfRange) {
        if (++bottomOutOfRange >= 2) {
          bottomOutOfRange = 0;

          setState(() {
            if (curPage < (totalPages - 1)) {
              curPage++;
              _pbVisible = true;
              SearchCars();
            } else {
              curPage = 0;
              _pbVisible = true;
              SearchCars();
            }
          });
        } else {
          _controller.animateTo(_controller.offset - 5,
              duration: Duration(milliseconds: 500), curve: Curves.easeIn);
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _context,
      appBar: CustomAppBar(),
      body: Container(
          margin: EdgeInsets.only(bottom: 82.0),
          child: SingleChildScrollView(
              controller: _controller,
....


First time user srolls to the very bottom of a page we bounce a little bit back and increment our counter, now if user really whishes to scroll down and see the next page our counter will be triggered one more time and we will know that it will be time to go to/append the next page.


Thank you and see you :)

 


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Android Xml Layout To Flutter Scaffold Converter - Completely free

Simple but may be useful

Android Xml Layout To Flutter Scaffold Converter - Completely free


If you need to quickly convert most of your Android XML layouts into Dart Scaffold files then you can just try this online free converter. It is not perfect and does not support all Android UI elements and containers but in most cases it will make your life easier and create much DART files for you to start migrating your Java UI to Flutter UI.


Android Xml Layout to Flutter Scaffold Converter


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y